我的思路是将启动路径写入注册表,在程序启动时执行将应用写入注册表,在下次重启时则会自动启动。
自启动执行代码
#region注册表自启动
try
{
string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
string path = Application.ExecutablePath.ToLower();
string name = Path.GetFileName(path);
var regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath, true);
if (regKey == null)
regKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(regPath);
regKey.SetValue(name, path);
}
catch { }
#endregion
项目右键 》 添加 》新建项 》

修改代码
<!-- UAC 清单选项
如果想要更改 Windows 用户帐户控制级别,请使用
以下节点之一替换 requestedExecutionLevel 节点。n
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
指定 requestedExecutionLevel 元素将禁用文件和注册表虚拟化。
如果你的应用程序需要此虚拟化来实现向后兼容性,则删除此
元素。
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
改成
项目右键 》 属性 》安全性 》
去掉 【 启用ClickOnce安全设置(N) 】勾选

把自己的应用程序生成后,放到服务器,加上日志,看看开机后是否自启动。
如果遇到问题,可以以管理员身份启动应用程序。
#region注册表自启动
try
{
string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
string path = Application.ExecutablePath.ToLower();
string name = Path.GetFileName(path);
var regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath, true);
if (regKey != null)
regKey.DeleteValue(name, false);
}
catch { }
#endregion
CMD 执行regedit
打开注册表后,Ctrl+F 查找程序名称
CMD执行regedit 注册表
开机启动项有四个可选也可以同时添加
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
右键 》 新建字符串值
数据里面写应用程序路径即可

打开计算机,右键计算机,点击管理

找到任务计划程序库,右边右键 新建任务

配置程序启动信息,安全选项》选则 不管用户是否登录都要运行

配置触发器,看什么时候执行

点击新建,配置执行时间

exe文件时后台静默启动
操作 》配置启动程序路径
