【发布时间】:2019-01-31 12:09:08
【问题描述】:
我想在启动时运行 C# 应用程序。我使用了这段代码,我找到了here:
private void SetStartup(bool enable)
{
string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(runKey);
if (enable)
{
if (startupKey.GetValue("ZanNews") == null)
{
startupKey.Close();
startupKey = Registry.CurrentUser.OpenSubKey(runKey, true);
startupKey.SetValue("ZanNews", "\"" + Application.ExecutablePath + "\"");
startupKey.Close();
}
}
else
{
startupKey = Registry.CurrentUser.OpenSubKey(runKey, true);
startupKey.DeleteValue("ZanNews", false);
startupKey.Close();
}
}
虽然该条目出现在注册表和任务管理器中,但该程序不会随 Windows 一起启动。
在问这个问题之前,我对 StackOverflow 进行了先前的研究,但提出的解决方案和代码 sn-ps here 和 here 都不起作用。要么我收到安全和访问错误消息,要么已写入注册表,但程序拒绝从操作系统启动。然而,我看到上述问题是在 2010 年和 2011 年提出的,我认为从那时起情况发生了变化。
有没有办法让程序在启动时运行?我在 2018 年 4 月 10 日更新的 Windows 上安装了 Windows 10 家庭版 1803 版和 .NET Framework 4.7.2。
稍后编辑:其他信息:
【问题讨论】:
-
你的跑得怎么样
SetStartup? -
请在您的命令提示符问题中附上屏幕截图,该命令提示符已在
C:\\Users\\alexz\\OneDrive\\Programe\\C#/ZanScore/ZanScore/bin/Debug/中运行dir(即向我们证明该文件夹存在,并且其中的可执行文件)。