简体   繁体   English

使用任务计划程序在 Windows 上计划 .Net Core 控制台应用程序

[英]Schedule a .Net Core console application on windows using Task Scheduler

是否可以使用任务计划程序安排 .net 核心控制台应用程序在每天的特定时间运行?

I think you will have to set the value for "Start in" while creating the task in task scheduler to your app folder, that is D:\Test\Test1.我认为您必须在将任务计划程序中的任务创建到您的应用程序文件夹时设置“开始于”的值,即 D:\Test\Test1。 The app will run in this folder and should be able to find the appsettings file.该应用程序将在此文件夹中运行,并且应该能够找到 appsettings 文件。

Yes, its possible.是的,有可能。 But theres a trick.但是有个窍门。 For some reason task scheduler mandates full path and filename for hosting process, so you need to enter in the "Program/Script" "C:\Program Files\dotnet\dotnet.exe".出于某种原因,任务调度程序要求托管进程的完整路径和文件名,因此您需要在“程序/脚本”中输入“C:\Program Files\dotnet\dotnet.exe”。 Confirmed this on Windows Server 2012 R2 with latest patches.在带有最新补丁的 Windows Server 2012 R2 上确认了这一点。 .Net Core program goes into arguments box, no need for full path there. .Net Core 程序进入参数框,那里不需要完整路径。 However .Net core program location path is required in "Start in".但是,“开始于”中需要 .Net 核心程序位置路径。 At least in cases when program is expecting to find some files from its location.至少在程序期望从其位置找到一些文件的情况下。

i have similar issue for 2 hours trying to get an answer for this.我有类似的问题 2 小时试图得到这个答案。

All you just need to do for your .Net Core Console App!您只需为您的 .Net Core 控制台应用程序做所有事情!

  • Go to your Task Scheduler转到您的任务计划程序
  • Go to the tab menu and click on Actions转到选项卡菜单并单击操作
  • Click on New/Edit点击新建/编辑
  • select the dropdown: Action: Start a program选择下拉菜单:操作:启动程序
  • program/script: "C:\Document\MyConsoleApp.exe"程序/脚本:“C:\Document\MyConsoleApp.exe”

------> note very important. ------> 注意很重要。 put in your app path where you have your console app because your task scheduler will have to read to configurations - Start in (optional): C:\Document\将您的应用程序路径放入您拥有控制台应用程序的位置,因为您的任务计划程序必须读取配置 - 开始(可选):C:\Document\

This solved my problem.这解决了我的问题。

Cheers Guys!!!干杯伙计们!!!

Create a .bat file with the contents "dotnet myDLL.dll" in actions program/script -> "c:\yourpath\myBatFile.bat" Start in --> "c:\yourpath"在操作程序/脚本 -> "c:\yourpath\myBatFile.bat" 中创建一个内容为 "dotnet myDLL.dll" 的 .bat 文件 -> "c:\yourpath"

done.完毕。

By default the app is going to look at the current folder for the existence of appsettings.json because of the following:默认情况下,应用程序将查看当前文件夹中是否存在appsettings.json ,原因如下:

    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();

replace the "appsettings.json" with the full path to the file."appsettings.json"替换为文件的完整路径。

Faced the same issue and it ate my hours, solved it by not publishing the project but only by building it.面临同样的问题,它占用了我的时间,通过不发布项目而仅通过构建它来解决它。 after building the project i just took the content of构建项目后,我刚刚获取了以下内容

MyProject\bin\Release\net5.0我的项目\bin\Release\net5.0

and placed it in并将其放入

F:\MyFolder F:\我的文件夹

then created a Task in Scheduler, with in Action => Program/script然后在调度程序中创建了一个任务,其中 in Action => Program/script

MyProject.exe我的项目.exe

and Start in开始

F:\MyFolder\MyProject\bin\Release\net5.0 F:\MyFolder\MyProject\bin\Release\net5.0

bingo...答对了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM