0
我想让我的应用程序在用户登录(系统启动)时启动。 我发现了这样的解决方案:Windows使应用程序在系统启动时运行(用户登录)
app.manifest
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
PreferencesWindows.xaml.cs
if (ServerSettings.ShouldApplicationAutostart)
{
SystemStartupService.AddThisAppToCurrentUserStartup();
} else
{
SystemStartupService.DeleteThisAppFromCurrentUserStartup();
}
SystemStartupService.cs
public static void AddThisAppToCurrentUserStartup()
{
AddAppToCurrentUserStartup(Assembly.GetExecutingAssembly().GetName().Name,
Assembly.GetExecutingAssembly().Location, true);
}
public static void AddAppToCurrentUserStartup(string appName, string appPath, bool asBackground) {
using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
registryKey.SetValue(appName, "\"" + appPath + "\"" + (asBackground ? " /background" : ""));
}
}
public static void DeleteThisAppFromCurrentUserStartup()
{
DeleteAppFromCurrentUserStartup(Assembly.GetExecutingAssembly().GetName().Name);
}
public static void DeleteAppFromCurrentUserStartup(string appName)
{
using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
registryKey.DeleteValue(appName, false);
}
}
问题是此解决方案不适用于我的应用程序,我不知道为什么。上面的代码成功添加条目注册表和系统配置>启动
如果它的事项,这是在通知试运行简单的WPF应用程序。
问题:应用程序无法在用户登录时启动!
我想你的appPath是错误的。 –
不,这是正确的,当我复制粘贴它指向与.exe文件的目录。我认为也许有调试版本的问题,所以我发布并将其复制到Program Files,并且它也不起作用 –
如果您将正确的文本放入注册表项中,此工作方式无需任何代码,因此您唯一可以做错了就是路径错误或文件无法访问。 –