The next code works on other operative systems, but not in Windows Vista:
TaskDefinition td = taskScheduler.NewTask();
td.Actions.Add(Application.ExecutablePath, string.Format("\"{0}\"", configPath));
td.Settings.DisallowStartIfOnBatteries = false;
td.Settings.StopIfGoingOnBatteries = false;
Task task = taskScheduler.RootFolder.RegisterTaskDefinition(taskName, td);
task.Definition.Principal.LogonType = TaskLogonType.Password;
task.Definition.Principal.RunLevel = TaskRunLevel.Highest;
TaskEditDialog editorForm = new TaskEditDialog(task, true, true);
editorForm.ShowDialog();
When clicking on OK button of the Task Edit Dialog, we are prompted to type password of the account. Even if the password is right, we get the error of the screenshot.
Comments: ** Comment from web user: Colecas **
I am not sure that the issue was the same as the other one. Because, I don't get an access denied error.
* The next code works:
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Does something";
// Create a trigger that will fire the task at this time every other day
td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));
// Register the task in the root folder
var t = ts.RootFolder.RegisterTaskDefinition(@"Test", td);
// If no exception thrown, then log success
output.WriteLine("Success! Task '{0}' created by '{1}' under '{2}'.", t.Name, t.Definition.RegistrationInfo.Author, t.Definition.Principal);
// Delete task
ts.RootFolder.DeleteTask(@"Test");
*But this one not (The only change is to open the TaskEditDialog):
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Does something";
// Create a trigger that will fire the task at this time every other day
td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));
// Register the task in the root folder
var t = ts.RootFolder.RegisterTaskDefinition(@"Test", td);
t.Definition.Principal.LogonType = TaskLogonType.Password;
TaskEditDialog editForm = new TaskEditDialog(t,true, true);
editForm.ShowDialog();
// If no exception thrown, then log success
output.WriteLine("Success! Task '{0}' created by '{1}' under '{2}'.", t.Name, t.Definition.RegistrationInfo.Author, t.Definition.Principal);
// Delete task
ts.RootFolder.DeleteTask(@"Test");