![](/img/trans.png)
[英]How to run a c# console application from windows task scheduler on server
[英]Windows C# Console Application Through Task Scheduler Won't Run Unless User Is Logged In
环境是 Windows Server 2016 Standard,使用在 Visual Studio 2019 中开发并通过 VS2019 发布向导发布的 C# 控制台应用程序。 奇怪的是,这似乎在我们的旧 Windows Server 2008 实例上运行良好,但自从我们升级到 2016 后就无法正常运行。
我开发了一个 C# 控制台应用程序来将数据添加到我的 SQL Server 数据库中。 它使用任务计划程序直接在数据库服务器上发布和运行。 如果我在计划任务运行时登录,没有问题。 但是,如果我没有登录,控制台应用程序就不会运行,而且我什至无法看到它能走多远。
任何有关如何使它与未登录用户一起运行的建议,或如何更好地解决问题所在的问题,我们将不胜感激。
目前作为测试,我想要做的就是启动应用程序,写入事件查看器应用程序日志和文本文件(除非登录,否则都不会发生)。
static void Main()
{
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Application";
eventLog.WriteEntry("Log message example", EventLogEntryType.Information, 101, 1);
}
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string status = "Start: " + DateTime.Now.ToString() + "...";
Log(status);
status = "Complete: " + DateTime.Now.ToString() + ". Press Enter to exit.";
Log(status);
}
public static void Log(string message)
{
// Create a writer and open the file:
StreamWriter log;
string LogFilePath = "C:\\[folder]\\LogFile.txt";
if (!File.Exists(LogFilePath))
{
log = new StreamWriter(LogFilePath);
}
else
{
log = File.AppendText(LogFilePath);
}
// Write to the file:
log.WriteLine(DateTime.Now);
log.WriteLine(message);
log.WriteLine();
// Close the stream:
log.Close();
Console.WriteLine("-Log Entry Added");
}
有趣的是,当我以未登录用户身份运行时,确实看到任务管理器中添加了一个 ClickOnce 进程,但仍然无法正常工作。
到目前为止,这是我尝试过的一些事情:
解决我的问题的东西:我从头开始重新创建控制台应用程序,但使用 .NET Core 3.0 框架创建。 复制我的代码并重新添加包。 然后我将这个新应用程序发布到服务器,它按预期工作。 仍然不确定将其归因于什么,但这解决了我的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.