Windows启动时启动窗口

我希望我的应用程序(WPF Window )在Windows启动时启动。 我尝试了不同的解决方案,但似乎没有人工作。 我必须在我的代码中写一下这样做?

当您说必须向注册表添加密钥时,您是对的。

添加密钥到:

 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 

如果要为当前用户启动应用程序。

要么:

 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 

如果您想为所有用户启动它。

例如,启动当前用户的应用程序:

 var path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; RegistryKey key = Registry.CurrentUser.OpenSubKey(path, true); key.SetValue("MyApplication", Application.ExecutablePath.ToString()); 

只需用第二行替换第二行

 RegistryKey key = Registry.LocalMachine.OpenSubKey(path, true); 

如果要在Windows启动时自动为所有用户启动应用程序。

如果您不想再自动启动应用程序,请删除注册表值。

因此:

 var path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; RegistryKey key = Registry.CurrentUser.OpenSubKey(path, true); key.DeleteValue("MyApplication", false); 

此示例代码已针对WinForms应用程序进行了测试。 如果您需要确定WPF应用程序的可执行文件的路径,请尝试以下操作。

 string path = System.Reflection.Assembly.GetExecutingAssembly().Location; 

只需将“Application.ExecutablePath.ToString()”替换为可执行文件的路径即可。