2011-05-08 37 views
3

工作在C#窗口application.I想写在registry.i知道如何写在registry.I使用bellow语法写在注册表上。写在注册表位置的方式

 Microsoft.Win32.RegistryKey key; 
     key = Microsoft.Win32.Registry.CurrentUser.cre.CreateSubKey("asb"); 
     key.SetValue("asb", "Isabella"); 
     key.Close(); 

但问题是我无法写在指定的位置。 我想在波纹管的位置

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run 

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run 

写在这个位置要添加字符串值=“ABC”ValueData =“EFD” 如果有任何疑问PLZ ask.thanks提前。

+0

哪个Windows版本在做这个失败?猜测它是失败的并且在Vista和Win7上给你一个ACCESS_DENIED? – slugster 2011-05-08 11:39:25

+0

通常安装程序写入'HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ CurrentVersion \ Run' – 2011-05-08 13:03:23

回答

7

对于HKCU:

string keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; 
RegistryKey rk = Registry.CurrentUser.OpenSubKey(keyName, true); 
rk.SetValue("abc", "efd"); 
rk.Close(); 

对于HKLM你需要管理员权限才能做到这一点。这需要在您的程序中添加一个清单,以在Vista或Win7上调用UAC提示。

2

写入HKEY_LOCAL_MACHINE需要管理权限。如果你在Windows Vista或7上运行,它也需要进程升级,以免与UAC(用户帐户控制)发生冲突。

最好的事情是只有在安装过程中写入此注册表项(您将拥有完全的管理权限)。安装应用程序后,您应该只从中读取

将所有常规设置保存在HKEY_CURRENT_USER下。使用Registry.CurrentUser field来做到这一点。或者,更好的是,完全放弃注册表和save your application's settings in a config file。 Visual Studio内置了对此的支持,从C#开始非常简单。注册表不再是保存应用程序状态的推荐方式。

2
RegistryKey reg = Registry.LocalMachine. 
        OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); 

// set value of "abc" to "efd" 
reg.SetValue("abc", "efd", RegistryValueKind.DWord); 

// get value of "abc"; return 0 if value not found 
string value = (string)reg.GetValue("abc", "0");