gpt4 book ai didi

c# - 在 Windows 启动时启动窗口

转载 作者:太空狗 更新时间:2023-10-29 18:18:52 24 4
gpt4 key购买 nike

我希望我的应用程序(WPF Window)在 Windows 启动时启动。我尝试了不同的解决方案,但似乎没有一个有效。我必须在我的代码中写些什么才能做到这一点?

最佳答案

您说的必须向注册表添加 key 是正确的。

将 key 添加到:

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()”替换为您的可执行文件的路径即可。

关于c# - 在 Windows 启动时启动窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com