如果您想在 Windows 启动时自动启动应用程序,则必须在 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);