0

Thought this would be a doddle. I have a number of Scheduled Tasks which are managed by the Task Scheduler. They have been configured to run on a schedule, but can also be run "on demand".

We want to funnel everything around these particular jobs through the Task Scheduler. As part of that, we want the ability to run the scheduled task from a Winforms App, a Console app etc. etc.

I can't seem to find a .NET 5 API for interacting with the Task Scehduler. I realize .NET is cross-platform now and this is very Windows-centric, but it would be nice if we could interact with something so fundamental to many systems out there.

onefootswill
  • 3,707
  • 6
  • 47
  • 101
  • 2
    No .NET (Framework, Core, etc.) includes a Task Scheduler API. You'll have to interop, some people worked on this, eg: https://github.com/dahall/taskscheduler – Simon Mourier Dec 17 '21 at 10:25
  • Have you seen this questions: https://stackoverflow.com/questions/7394806/creating-scheduled-tasks ? You can access WinRT APIs from .Net 5 (e.g. https://blogs.windows.com/windowsdeveloper/2020/09/03/calling-windows-apis-in-net5/) – bavaza Dec 19 '21 at 16:33

2 Answers2

1

For running from commandline / console this should help: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks

feddoh
  • 31
  • 7
0

try

            var processStartInfo = new ProcessStartInfo
            {
                Arguments = Arguments(),
                CreateNoWindow = true,
                FileName = Executable,
                ...
            };
            var process = System.Diagnostics.Process.Start(processStartInfo);

to run

schtasks.exe /run /TN TaskName