string subKeyName = @"SoftwareMicrosoftWindowsCurrentVersionRun"; //subkey name
string valueName = @"App Name"; //name of the more specific key that will hold the value, "" means (Default)
try
...{
RegistryKey reg = Registry.LocalMachine.OpenSubKey(subKeyName);
if (reg != null)
...{
reg.DeleteValue(valueName);
reg.Close();
}
}
catch (Exception ex)
...{
MessageBox.Show(this, ex.ToString());
}
执行这段代码,你可以会收到以下异常:
System.UnauthorizedAccessException
原因很简单:
RegistryKey.OpenSubKey (String) 以只读方式检索子项
public RegistryKey OpenSubKey ( string name, bool writable)writable如果需要项的写访问权限,则设置为 true。
我们需要带第二个参数,标示我们是可写方式打开的。