27

Is it possible to schedule a .net core console application to run every day at a specific time using the Task Scheduler?

Sandhya
  • 606
  • 1
  • 7
  • 13
  • 3
    yes, it is possible. Describe what you have tried so far and what is the current problem. Right now your question is off-topic. – Set Apr 24 '17 at 18:29
  • My console application is in the folder D:\Test\Test1. I can run the program from within this folder: dotnet MyApp.dll. However I am not sure how to run it from outside the published folder. I get an error when I try to run it from outside like this : dotnet D:\Test\Test1\MyApp.dll. The error says it cannot find appsettings.json. What is the right syntax to run a .net core console app from outside the published folder? – Sandhya Apr 24 '17 at 18:45
  • You then need to show the code where you define that your program should read appsettings.json – Scott Chamberlain Apr 24 '17 at 19:09
  • 2
    Why down voted? Everyone may not know the answers which are trivial to you. – Hemant Sakta Sep 07 '17 at 20:51

6 Answers6

53

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. The app will run in this folder and should be able to find the appsettings file.

14

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". Confirmed this on Windows Server 2012 R2 with latest patches. .Net Core program goes into arguments box, no need for full path there. However .Net core program location path is required in "Start in". At least in cases when program is expecting to find some files from its location.

Olavi Vaino
  • 422
  • 5
  • 10
  • 1
    IMHO this is the most complete answer. I realize it came along well after the others, but it has the details that aren't fully clear in the other answers. Especially for people like me who are used to the "old way" of scheduling console applications. – Mike Devenney Oct 18 '19 at 12:50
  • 1
    Start In for the win.... couldn't find my appsettings.json file.... :-) Set Start In and worked. – Kevin LaBranche Jun 16 '21 at 17:32
  • This really helped me. Thanks – Vikas Nale Jul 05 '23 at 06:57
9

i have similar issue for 2 hours trying to get an answer for this.

All you just need to do for your .Net Core Console App!

  • 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"

------> 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\

This solved my problem.

Cheers Guys!!!

Timonology
  • 131
  • 1
  • 5
2

Create a .bat file with the contents "dotnet myDLL.dll" in actions program/script -> "c:\yourpath\myBatFile.bat" Start in --> "c:\yourpath"

done.

TheStripe
  • 31
  • 1
1

By default the app is going to look at the current folder for the existence of appsettings.json because of the following:

    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.

Muqeet Khan
  • 2,094
  • 1
  • 18
  • 28
  • Actually shouldn't the `.SetBasePath(env.ContentRootPath)` make it so it does not use the current folder. but actually looks at the base path of the application? I bet the problem is the OP is just missing that line. – Scott Chamberlain Apr 24 '17 at 21:44
  • @ScottChamberlain it depends on how he has configured his webhostbuilder I think. By default, the webhost builder uses `.UseContentRoot(Directory.GetCurrentDirectory())` I believe... so wouldn't that make setbasepath to be incorrect as well? – Muqeet Khan Apr 24 '17 at 22:12
  • 1
    Thanks for the suggestions! I was just not setting the 'start in' folder in the Task scheduler - that was the problem. And its a she btw.. – Sandhya Apr 24 '17 at 22:16
  • Voted up. The answer provided is helpful for the question asked – codingpirate Nov 08 '17 at 14:13
  • Voted down. The answer provided in no way answers the OP's question. – Mike Devenney Oct 18 '19 at 12:48
0

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

and placed it in

F:\MyFolder

then created a Task in Scheduler, with in Action => Program/script

MyProject.exe

and Start in

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

bingo...

Muhammad Zakaria
  • 1,269
  • 6
  • 14