如何在c#

时间:2016-01-13 05:16:15

标签: c# task

以下是我想做任务的条件

安全选项

  1. 帐户:SYSTEM
  2. 运行用户是否已登录
  3. 以最高权限运行。
  4. 当我创建任务调度程序时,发生异常(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);
                            }
                        }
    }
    

1 个答案:

答案 0 :(得分:0)

拒绝访问意味着您无权执行此操作。

除非您是管理员,否则无法创建以SYSTEM身份运行的任务。您需要从提升的命令提示符运行此程序。