Reputation: 6979
I am trying to schedule the tasks from my application using schtasks
instead of using the Windows Task Scheduler COM/managed API.
Creating a task here is easier by using this on command line schtasks /Create /tn "MyApp" /tr c:\myapp.exe /sc onlogon
but I opened the command line as an administrator to get the task created (otherwise I get access denied)
From my application, to create the task I use
string args = @"/Create /tn MyApp /tr c:\myapp.exe /sc onlogon";
Process.Start("schtasks", args);
However, the task gets created only if I run my application as an administrator. I need to avoid this, so that any user can create the task without the hassle of running the app as admin. Any suggestions on how ca this be done?
Upvotes: 3
Views: 9844
Reputation: 2763
Contents of TextBox: "SCHTASKS.exe /Create /ST 06:30 /SC DAILY /TN report /TR notepad.exe"
I was able to use Process.Start(ProcessStartInfo);
ProcessStartInfo psinfo = new ProcessStartInfo();
psinfo.Filename = "powershell.exe";
psinfo.Args = textbox1.text;
Process.Start(psinfo);
**Of course, this will only work on machines that have powershell installed, using cmd.exe failed to create the scheduled task...
Upvotes: 0
Reputation: 10054
Why should that require user interruption each time in an exhibition booth PC which is mounted just for exposition purpose?
If you want a hidden program that will run on a PC whenever it is booted, and your task is not harmful, you can add it to the following registry key:
HKCU\Software\Windows\CurrentVersion\Run
programmatically. I can provide code if needed.
Upvotes: 0
Reputation: 72636
You can try with a sort of RunAs that allow to run a process with a specified user :
ProcessStartInfo psi = new ProcessStartInfo("schtasks");
psi.UseShellExecute = false;
psi.UserName = "Username";
psi.Password = "password";
Process.Start(psi);
The Process class through ProcessStartInfo provides a mechanism which allows you to specify the user context that the new process should run under, so you can specify the user in which you want to run the command even if the user that start the program is different. In your case you can specify Administrator credentials to the ProcessStartInfo without having to run the program with an Administrator user ...
Upvotes: 2