使用VC,VB等語言操作注冊表的例子已經有很多了,其實在C#裡操作注冊表更加的簡單方便。下面的例子就提供了在C#裡操作注冊表的方法:
using Microsoft.Win32;
using System.Diagnostics;
private void Access_Registry()
{
// 在HKEY_LOCAL_MacHINE\Software下建立一新鍵,起名為MCBInc
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
// 增加一個子鍵
RegistryKey newkey = key.CreateSubKey("MCBInc");
// 設置此子鍵的值
newkey.SetValue("MCBInc", "NET Developer");
// 從注冊表的其他地方獲取數據
// 找出你的CPU
RegistryKey pRegKey = Registry.LocalMachine;
pRegKey = pRegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
Object val = pRegKey.GetValue("VendorIdentifIEr");
Debug.WriteLine("The central processor of this Machine is:"+ val);
// 刪除鍵值
RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software", true);
delKey.DeleteSubKey("MCBInc");
}