vb.net 使用VB.net创建计划任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20677722/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:11:57  来源:igfitidea点击:

Create scheduled task using VB.net

.netvb.nettaskschedulewindows-task-scheduler

提问by Steven Trainor

How do I create a scheduled task using VB.NET - Populating Scheduled Task fields from vb.net program on button click?

如何使用 VB.NET 创建计划任务 - 单击按钮时从 vb.net 程序填充计划任务字段?

I have nothing at the moment, nor do I even know if it is possible.

我现在一无所有,我什至不知道是否有可能。

回答by meziantou

You have to create wrappers around the native COM interfaces. If you don't want to do it yourself, you can use this library https://taskscheduler.codeplex.com

您必须围绕本机 COM 接口创建包装器。如果不想自己动手,可以使用这个库https://taskscheduler.codeplex.com

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[] args)
   {
      // Get the service on the local machine
      using (TaskService ts = new TaskService())
      {
         // 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
         ts.RootFolder.RegisterTaskDefinition(@"Test", td);

         // Remove the task we just created
         ts.RootFolder.DeleteTask("Test");
      }
   }
}