C#操作注册表,项目名为空

今天,忽然想写一个设置默认浏览器的小的C#程序,于是就要操作注册表,可是结果发现注册表的该项的项目名为空的,且不能修改,后来使用string.Empty代替了SetValue参数中的name,结果就OK了。

/*
 * Created by SharpDevelop.
 * User: Peng QianHe
 * Date: 9/8/2012
 * Time: 8:30 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using Microsoft.Win32;

namespace SetDefaultBrowser
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void BtnChooseExeClick(object sender, EventArgs e)
		{
			OpenFileDialog ofd=new OpenFileDialog();
			ofd.Filter="EXE File(*.exe)|*.exe";
			if(ofd.ShowDialog()==DialogResult.OK)
			{
				label1.Text=ofd.FileName;
			}
		}
		
		void BtnSetDefaultClick(object sender, EventArgs e)
		{
			RegistryKey pregkey = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command",true);
			
			try{
				if(label1.Text != string.Empty && label1.Text.Trim().Length>0){
					pregkey.SetValue(string.Empty,"\""+label1.Text+"\" -- \"%1\"");
				}else{
					MessageBox.Show("Choose a EXE file at first.","Error");
				}
			}catch(Exception ex){
				MessageBox.Show(ex.Message);
			}finally{
				pregkey.Close();
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值