如何解决c#中的创建任务错误
本文关键字:创建 任务 错误 何解决 解决 | 更新日期: 2023-09-27 18:30:01
我想让任务的条件如下
安全选项
- 帐户:SYSTEM
- 无论用户是否登录都运行
- 以最高权限运行
创建任务调度程序时,出现异常(HRESULT:0x80070005)E_ACESSDENIED
private void CreateTask()
{
try
{
using (TaskService ts = new TaskService())
{
var newTask = ts.NewTask();
newTask.RegistrationInfo.Author = Environment.UserName;
newTask.RegistrationInfo.Description = "Crfs Bin Upload";
newTask.Principal.UserId = new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null).Translate(typeof(NTAccount)).Value;
newTask.Principal.RunLevel = TaskRunLevel.Highest;
Trigger trigger = new DailyTrigger();
trigger.StartBoundary = DateTime.Now.AddMinutes(5);
trigger.Repetition.Interval = TimeSpan.FromMinutes(5);
trigger.Repetition.StopAtDurationEnd = false;
trigger.Enabled = true;
newTask.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
newTask.Settings.DisallowStartIfOnBatteries = true;
newTask.Settings.StopIfGoingOnBatteries = false;
newTask.Settings.AllowHardTerminate = false;
newTask.Settings.StartWhenAvailable = false;
newTask.Settings.RunOnlyIfNetworkAvailable = true;
newTask.Settings.IdleSettings.StopOnIdleEnd = false;
newTask.Settings.IdleSettings.RestartOnIdle = false;
newTask.Settings.Enabled = true;
newTask.Settings.Hidden = false;
newTask.Settings.RunOnlyIfIdle = false;
newTask.Settings.WakeToRun = false;
newTask.Settings.ExecutionTimeLimit = TimeSpan.FromSeconds(0);
newTask.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal;
newTask.Triggers.Add(trigger);
newTask.Actions.Add(new ExecAction("notepad.exe"));
ts.RootFolder.RegisterTaskDefinition("NewTask", newTask);
MessageBox.Show("작업 스케줄러가 정상적으로 등록되었습니다.", "작업 스케줄러 등록", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
catch (Exception ex)
{
MessageBox.Show("작업 스케줄러의 등록을 실패하였습니다.'r'n" + ex.Message, "작업 스케줄러 등록 실패", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
拒绝访问意味着您无权执行此操作。
除非您是管理员,否则您不能创建要作为SYSTEM运行的任务。您需要从提升的命令提示符运行此程序。