本文介绍了C# - 使用具有的Process.Start计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图整合计划作业语句转换的Process.Start

 的Process.Start(SchTasks.exe会 \+ textBox1.Text +\); 

如何将有可能低于添加参数到上面的Process.Start声明?

 的schtasks /创建/ SC每日/ TN TestJob / TRC:\Program Files\test\test.exe'C: \'


解决方案

您可以使用Windows任务交互经理直接使用的TaskScheduler 。它会给你进入一个全系列任务的性质和在什么条件下会被解雇。这,当然需要更多的代码,但它给你一切,你在的 managaged方式



这是一块需要的控制的,即时通讯使用自己和它运作良好的代码(IVE切掉我的一些业务逻辑,所以不是所有的参数将编译/有意义的)。它基本上会创建将火一把分钟可抵达现在的任务:

  TaskScheduler.TaskScheduler调度=新TaskScheduler.TaskScheduler(); 
scheduler.Connect(NULL,NULL,NULL,NULL); //运行作为当前用户。

ITaskDefinition的taskdef = scheduler.NewTask(0);
taskDef.RegistrationInfo.Author =我我我;
taskDef.RegistrationInfo.Description =我的说明;
taskDef.Settings.ExecutionTimeLimit =PT10M; //10分钟
taskDef.Settings.DisallowStartIfOnBatteries = FALSE;
taskDef.Settings.StopIfGoingOnBatteries = FALSE;
taskDef.Settings.WakeToRun = TRUE;

ITimeTrigger触发=(ITimeTrigger)taskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);

的DateTime nextRun = DateTime.Now.AddMinutes(1);从现在//1分钟
trigger.StartBoundary = nextRun.ToString(S,System.Globalization.CultureInfo.InvariantCulture);

IExecAction行动=(IExecAction)taskDef.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
action.Id =EXE名;
action.Path =路径exe文件;
action.WorkingDirectory =工作目录;
action.Arguments =应用程序参数 ///< - 在这里你把你的论点..

ITaskFolder根= scheduler.GetFolder(\\);

IRegisteredTask regTask = root.RegisterTaskDefinition(
我的任务名,
的taskdef,
(INT)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
空,//用户
空,//密码
_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN,//用户必须已经登录。该任务将只在现有的交互式会话中运行。
// SDDL
);



更多的解释和代码示例可以在这里找到:的


的Windows Vista(和Windows Server 2008)

I am trying to integrate a scheduled job statement into Process.Start

Process.Start("schtasks.exe", "\"" + textBox1.Text + "\"");

How would it be possible to add the parameters below into the Process.Start statement above?

schtasks /Create /SC DAILY /TN TestJob /TR "C:\Program Files\test\test.exe 'C:\'"
解决方案

You can interact with the windows task manager directly using TaskScheduler. It will give you access to a whole range of properties of the task and under what conditions it will be fired. It, of course, require more code, but it gives you all the control that you need in a managaged manner.

This is a piece of code that im using myself and it is working well (ive cut away some of my business logic so not all arguments will compile/make sense). It will basically create a task that will fire one minute from Now:

    TaskScheduler.TaskScheduler scheduler = new TaskScheduler.TaskScheduler();
    scheduler.Connect(null, null, null, null); //run as current user.

    ITaskDefinition taskDef = scheduler.NewTask(0);
    taskDef.RegistrationInfo.Author = "Me me me";
    taskDef.RegistrationInfo.Description = "My description";
    taskDef.Settings.ExecutionTimeLimit = "PT10M"; // 10 minutes
    taskDef.Settings.DisallowStartIfOnBatteries = false;
    taskDef.Settings.StopIfGoingOnBatteries = false;
    taskDef.Settings.WakeToRun = true;

    ITimeTrigger trigger = (ITimeTrigger)taskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);

    DateTime nextRun = DateTime.Now.AddMinutes(1); // one minute from now
    trigger.StartBoundary = nextRun.ToString("s", System.Globalization.CultureInfo.InvariantCulture);

    IExecAction action = (IExecAction)taskDef.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
    action.Id = "exe name";
    action.Path = "path to exe";
    action.WorkingDirectory = "working dir";
    action.Arguments = "app arguments";  /// <-- here you put your arguments..

    ITaskFolder root = scheduler.GetFolder("\\");

    IRegisteredTask regTask = root.RegisterTaskDefinition(
        "My task name",
        taskDef,
        (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
        null, // user
        null, // password
        _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, //User must already be logged on. The task will be run only in an existing interactive session.
        "" //SDDL
        );

More explaination and code samples can be found here: Calling the Task Scheduler in Windows Vista (and Windows Server 2008) from managed code

这篇关于C# - 使用具有的Process.Start计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:36