这段代码:
RegistryKey rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rKey.DeleteValue(Application.ProductName, false);
rKey.SetValue(Application.ProductName, Application.ExecutablePath, RegistryValueKind.String);
在 Windows 8 上不起作用。我不知道为什么,因为在 Windows 7 和 Windows XP 上此解决方案有效。
你能帮帮我吗?
请您参考如下方法:
为了在注册表中设置某些内容,您需要以管理员身份运行该应用程序。 为此,您首先要添加 Application Manifest File到项目中的属性“文件夹”。
然后你改变
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
收件人:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
那我不知道你获取当前可执行文件路径的方式是否正确,对我来说至少这样有效:
class Program
{
private static void RegisterAsRun()
{
string exePath = new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath;
Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "TestApp", exePath, RegistryValueKind.String);
}
static void Main(string[] args)
{
RegisterAsRun();
Console.WriteLine("Hello!");
Console.ReadLine();
}
}
另一个注意事项是,如果应用程序是在 x86 中编译的并且操作系统是 x64,则注册表项将最终位于 Wow64 注册表中,这使其成为以下路径:
HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run
