如何创建一个控制台应用程序以在 Windows 启动时自动运行

Var*_*han 0 .net c# console-application

如何用C#创建一个Windows启动时自动运行的控制台应用程序?我怎样才能以编程方式做到这一点。我尝试了这个......

 RegistryKey rkApp = 
 Registry.LocalMachine.OpenSubKey
 ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run   ", true);
 rkApp.SetValue("MyAPP", Assembly.GetExecutingAssembly().Location);
Run Code Online (Sandbox Code Playgroud)

但在当前上下文中找不到 SetValue 方法。

小智 5

使用 CurrentUser 而不是 LocalMachine:

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Run Code Online (Sandbox Code Playgroud)

删除 \Run 后面的空格,并使用以下命令设置值:

rkApp.SetValue("MyApp", Application.ExecutablePath);
Run Code Online (Sandbox Code Playgroud)