C#读取和创建注册表的某一项
C#中注册表的操作使用RegistryKey类,创建,读取和判断某项是否存在。
1、读取某一项的值,以判断.NET Framework 2.0.50727是否安装为例。
我们知道看.net是否安装看HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727下的Install的值是否为1。那么如何读取呢?下面的代码可以实现。
RegistryKey machinelocalItem;
RegistryKey softwareItem;
RegistryKey microSoftItem;
RegistryKey docNetItem;
RegistryKey ndpItem;
RegistryKey versionItem;
machinelocalItem = Registry.LocalMachine;
softwareItem = machinelocalItem.OpenSubKey("SOFTWARE");
microSoftItem = softwareItem.OpenSubKey("Microsoft");
docNetItem = microSoftItem.OpenSubKey("NET Framework Setup");
ndpItem = docNetItem.OpenSubKey("NDP");
versionItem = ndpItem.OpenSubKey("v2.0.50727" true);
object installValue = netItem.GetValue("Install");
;
RegistryKey machinelocalItem;
RegistryKey softwareItem;
RegistryKey microSoftItem;
RegistryKey docNetItem;
RegistryKey ndpItem;
RegistryKey versionItem;
machinelocalItem = Registry.LocalMachine;
softwareItem = machinelocalItem.OpenSubKey("SOFTWARE");
microSoftItem = softwareItem.OpenSubKey("Microsoft");
docNetItem = microSoftItem.OpenSubKey("NET Framework Setup");
ndpItem = docNetItem.OpenSubKey("NDP");
versionItem = ndpItem.OpenSubKey("v2.0.50727" true);
object installValue = netItem.GetValue("Install");
完成了读取。当然这是已知这些项都存在的,如果不确定的项,OpenSubKey时判断用来读取的项是否NULL就可以了。
2、创建某项。
还是以上面的为例创建。
RegistryKey machinelocalItem;
RegistryKey softwareItem;
RegistryKey microSoftItem;
RegistryKey docNetItem;
RegistryKey ndpItem;
RegistryKey versionItem;
machinelocalItem = Registry.LocalMachine;
softwareItem = machinelocalItem.CreateSubKey("Software");
microSoftItem = softwareItem.CreateSubKey("Microsoft");
docNetItem = microSoftItem.CreateSubKey("NET Framework Setup");
ndpItem = docNetItem.CreateSubKey("NDP");
versionItem = ndpItem.CreateSubKey("v2.0.50727");
versionItem.SetValue("Install", 1);
RegistryKey machinelocalItem;
RegistryKey softwareItem;
RegistryKey microSoftItem;
RegistryKey docNetItem;
RegistryKey ndpItem;
RegistryKey versionItem;
machinelocalItem = Registry.LocalMachine;
softwareItem = machinelocalItem.CreateSubKey("Software");
microSoftItem = softwareItem.CreateSubKey("Microsoft");
docNetItem = microSoftItem.CreateSubKey("NET Framework Setup");
ndpItem = docNetItem.CreateSubKey("NDP");
versionItem = ndpItem.CreateSubKey("v2.0.50727");
versionItem.SetValue("Install", 1);
完成了创建并赋值。
摘自 白杨树
补充:软件开发 , C# ,