I have a question about register task scheduler in ASP.NET Web Page.
I have a execution file.
And I want to run this program once a day.
On my web page, the user can set what time to run this program.
So
Here is my code snippets in Codebehind.
txt_timeset is the time edit control.
using Microsoft.Win32.TaskScheduler;
...
try
{
TaskDefinition taskDef;
using (Task task = TaskService.Instance.GetTask(taskName))
{
if (task != null)
taskDef = task.Definition;
else
taskDef = TaskService.Instance.NewTask();
}
taskDef.Triggers.Clear();
taskDef.Actions.Clear();
DailyTrigger dt = new DailyTrigger();
DateTime startDate = DateTime.Today.AddHours(txt_timeset.DateTime.Hour);
startDate = startDate.AddMinutes(txt_timeset.DateTime.Minute);
dt.StartBoundary = startDate;
dt.DaysInterval = 1;
taskDef.Triggers.Add(dt);
taskDef.RegistrationInfo.Description = "Task Description";
string exePath = Server.MapPath("~/bin/Alarm/SendEmailTask.exe");
taskDef.Actions.Add(exePath, "");
TaskService.Instance.RootFolder.RegisterTaskDefinition(taskName, taskDef);
}
catch (Exception err)
{
string errMsg = err.ToString();
}
This code catch the exception.
thie errMsg is like this
System.Runtime.InteropServices.COMException (0x80070534): (45,4):UserId: at Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.RegisterTaskDefinition(String Path, ITaskDefinition pDefinition, Int32 flags, Object UserId, Object password, TaskLogonType LogonType, Object sddl) at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinitipn definition, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl) at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition)
So I put the administors's id and password to the arguments of the RegisterTaskDefinition.
TaskService.Instance.RootFolder.RegisterTaskDefinition
(taskName, taskDef, TaskCreation.CreateOrUpdate, "Administrator", "mypassword");
It work's well. However, the passwords of admin change frequently.
I want to RegisterTaskDefinition without admin's id and password.
Are there another way to set task scheduler in asp.net?
Please give me advices.
Thank you.
User contributions licensed under CC BY-SA 3.0