今天,忽然想写一个设置默认浏览器的小的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();
}
}
}
}