How to set "run only at login" and "run as" using TaskScheduler in C #?

I have code that uses a C # TaskManager object to create a task. In Windows 7, it works fine, but in Windows XP (and, presumably, another Windows), it does not work at all, because the default user for this task is the system and, therefore, there is no session to display the graphical interface. If I change the created task manually in the widgets of the control panel so that the task is executed only when the user logs in and only for a specific user, then everything works fine. But, despite the hours of searching, I see no options for setting these parameters in C # objects. Does anyone know a solution with existing objects? I would not want to rewrite everything to manually launch the EXE scheduler and transfer stuff on the command line.

Q

+5
source share
1 answer

Ok, I get the answer!

I did not understand this, but I used a third-party task scheduler manager (it has been a while since I wrote this part of my code), and this explains why the help was hard to find! I stumbled upon this page a minute ago, and right there, in their examples, was what I needed! A detailed solution can be found here , but the key part is

// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.Principal.UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
td.Principal.LogonType = TaskLogonType.InteractiveToken;

Thanks for the help!

+4
source

All Articles