[英]C# application does not start on windows startup
我正在尝试在Windows启动时启动我的应用程序,而且我也在尝试启动sql服务(如果未启动则运行正常),但是当我重新启动系统时,应用程序无法启动
在启动时注册
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run", true);
key.SetValue(“ Attendence”,Application.ExecutablePath.ToString());
启动SQL服务
string PC_Name = System.Environment.GetEnvironmentVariable("COMPUTERNAME"); System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("MSSQL$SQLEXPRESS",PC_Name); if (sc.Status.Equals(ServiceControllerStatus.Stopped) || sc.Status.Equals(ServiceControllerStatus.StartPending)) { sc.Start(); System.Threading.Thread.Sleep(5000); }
<applicationRequestMinimum>
<defaultAssemblyRequest permissionSetReference="Custom" />
<PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
</applicationRequestMinimum>
更改:
key.SetValue("Attendence", Application.ExecutablePath.ToString());
至:
key.SetValue("Attendence", "\"" + Application.ExecutablePath.ToString() + "\"");
在路径周围提供必要的引号(因为路径中有空格)。
这可能有效,但是如果您需要立即启动而不是启动程序,则将其放入Form1_Load代码块中
码:
using (Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("YourExecutable", "\"" + Application.ExecutablePath + "\"");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.