windows C# - 在 Process.Start 中使用计划任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6402969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - Using scheduled tasks with Process.Start
提问by James
I am trying to integrate a scheduled job statement into Process.Start
我正在尝试将预定的作业语句集成到 Process.Start
Process.Start("schtasks.exe", "\"" + textBox1.Text + "\"");
How would it be possible to add the parameters below into the Process.Start statement above?
如何将下面的参数添加到上面的 Process.Start 语句中?
schtasks /Create /SC DAILY /TN TestJob /TR "C:\Program Files\test\test.exe 'C:\'"
回答by Avada Kedavra
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.
您可以直接使用TaskScheduler
. 它将使您可以访问任务的所有属性以及在什么条件下将被触发。当然,它需要更多代码,但它以托管方式为您提供所需的所有控制。
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
更多解释和代码示例可以在这里找到:Calling the Task Scheduler in Windows Vista (and Windows Server 2008) from managed code
回答by Davy8
The problem is you need to double-escape.
问题是你需要双重逃避。
The proper command at the command line for:
命令行中的正确命令:
schtasks /Create /SC DAILY /TN TestJob /TR "C:\Program Files\test\test.exe 'C:\'"
is
是
schtasks /Create /SC DAILY /TN TestJob /TR "\"C:\Program Files\test\test.exe\" \"C:\\""
So that means you'll need:
所以这意味着你需要:
Process.Start("schtasks.exe", string.Format(@"/Create /SC DAILY /TN TestJob /TR """"{0}"" ""{1}""""", textBox1.Text.Replace("\", "\"), @"C:\"));
(Don't have a compiler handy so there may be typos, but you should get the idea.) I'm making the assumption that textBox
contains the path to the exe, not sure where the parameter is coming from or if it's hard coded.
(手边没有编译器,所以可能会有拼写错误,但你应该明白这个想法。)我假设textBox
包含到 exe 的路径,不确定参数来自哪里或者它是否是硬编码的。