Windows启动时启动窗口

Nic*_*ick 8 c# wpf startup

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

Chr*_*ers 14

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

添加密钥到:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Run Code Online (Sandbox Code Playgroud)

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

要么:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 
Run Code Online (Sandbox Code Playgroud)

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

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

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

只需用第二行替换第二行

RegistryKey key = Registry.LocalMachine.OpenSubKey(path, true);
Run Code Online (Sandbox Code Playgroud)

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

如果您不想再自动启动应用程序,只需删除注册表值即可.

因此:

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

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

string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
Run Code Online (Sandbox Code Playgroud)

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

  • 这个代码需要为WPF应用程序编写在哪里? (2认同)