在VC#中提供了Registry类、RegistryKey类来实现对注册表的操作。
其中Registry类封装了注册表的七个基本主健:
Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键
Registry.CurrentUser 对应于HKEY_CURRENT_USER主键
Registry.LocalMachine 对应于 HKEY_LOCAL_MACHINE主键
Registry.User 对应于 HKEY_USER主键
Registry.CurrentConfig 对应于HEKY_CURRENT_CONFIG主键
Registry.DynDa 对应于HKEY_DYN_DATA主键
Registry.PerformanceData 对应于HKEY_PERFORMANCE_DATA主键
RegistryKey类封装了对注册表的基本操作,包括读取,写入,删除。
其中读取的主要函数有:
OpenSubKey ( string name )方法主要是打开指定的子键。
GetSubKeyNames ( )方法是获得主键下面的所有子键的名称,它的返回值是一个字符串数组。
GetValueNames ( )方法是获得当前子键中的所有的键名称,它的返回值也是一个字符串数组。
GetValue ( string name )方法是指定键的键值。
写入的函数:
CreateSubKey(string name)方法是增加一个子键
SetValue(string name,string value)方法是设置一个键的键值
删除的函数:
DeleteSubKey ( )方法:删除一个指定的子键。
DeleteSubKeyTree ( )方法:此方法是彻底删除指定的子键目录,即:删除该子键以及该子键以下的全部子键。
在下面的例子里,要注意:
建项函数:CreateSubKey()
建子键函数:SetValue();

class Program


{

static string Generateguid()//建立GUID值的函数


{

return System.Guid.NewGuid().ToString();

}

static void Main(string[] args)


{

RegistryKey pregkey;

string preName;

//pregkey = Registry.CurrentUser.OpenSubKey("Control Panel\\\\Desktop\\\\WindowMetrics",true);

pregkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\\\Microsoft\\\\Internet Explorer\\\\Extensions",true);//以只读方式检索子项

if (pregkey == null)


{

Console.WriteLine("该键值不存在");

}

else


{

preName = Generateguid();

Console.WriteLine(preName);

Console.WriteLine("找到该键值");

preName = "{" + preName + "}";

//pregkey.SetValue("MinAnimate","1");

pregkey.CreateSubKey(preName);//建立子项

pregkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\\\Microsoft\\\\Internet Explorer\\\\Extensions\\\\"+preName, true);

//pregkey.SetValue("bbbbb", "0");//建立子键,如果有此子键,则设值。

pregkey.SetValue("ButtonText", "添加");//鼠标放到bar上时显示的文字

pregkey.SetValue("CLSID", "{1FBA04EE-3024-11d2-8F1F-0000F87ABD16}");//CLSID固定

pregkey.SetValue("Default Visible", "yes");//默认可看见

pregkey.SetValue("Exec", "D:/IEbar/IEpinout.exe");//关联的exe文件

pregkey.SetValue("HotIcon", "D:/IEbar/Icon2.ico");//鼠标放到bar上时显示的图样

pregkey.SetValue("Icon", "D:/IEbar/Icon1.ico");//鼠标未放在bar上时显示的图样

//pregkey.SetValue("MenuStatusBar", "杨");

//pregkey.SetValue("MenuText", "我是杨!");

}

pregkey.Close();

}