如何使用 C# 验证服务器上是否存在计划任务?

发布于 2024-07-30 14:12:39 字数 29 浏览 1 评论 0原文

另外你如何判断它是否正在运行、启用/禁用等?

also how can you tell if it is running, enabled/disabled, etc.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千紇 2024-08-06 14:12:39

您可以使用任务计划程序 API访问有关任务的信息。 (它是一个 com 库,但您可以使用 pinvokes 从 C# 调用它)

有一篇 文章在 codeproject 上,它为 API 提供了 .net 包装器。

[还有 schtask 命令 - 更多信息]

There is a Task Scheduler API that you can use to access information about tasks. (It is a com library, but you can call it from C# using pinvokes)

There is an article on codeproject that provides a .net wrapper for the API.

[There is also the schtasks command - more info]

神经大条 2024-08-06 14:12:39

该 API 可以通过 NuGet 安装。 包名称是“TaskScheduler”。 (https://github.com/dahall/taskscheduler)

以下示例展示了如何测试是否任务使用其名称进行安装,如果没有安装,则每小时执行一次。

using (TaskService service = new TaskService())
{
    if (!service.RootFolder.AllTasks.Any(t => t.Name == "YourScheduledTaskName"))
    {
        Console.WriteLine("YourScheduledTaskName is not installed on this system. Do you want to install it now? (y/n)");
        var answer = Console.ReadLine();
        if (answer == "y")
        {
            var task = service.NewTask();
            task.RegistrationInfo.Description = "YourScheduledTaskDescription";
            task.RegistrationInfo.Author = "YourAuthorName";

            var hourlyTrigger = new DailyTrigger { StartBoundary = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 1, 0 , 0) };
            hourlyTrigger.Repetition.Interval = TimeSpan.FromHours(1);
            task.Triggers.Add(hourlyTrigger);

            var taskExecutablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "YourScheduledTaskName.exe");
            task.Actions.Add(new ExecAction(taskExecutablePath));

            service.RootFolder.RegisterTaskDefinition("YourScheduledTaskName", task);
        }
    }
}

The API can be installed via NuGet. The package name is 'TaskScheduler'. (https://github.com/dahall/taskscheduler)

The following example shows how you can test whether a task is installed using its name and if not install it with an hourly execution.

using (TaskService service = new TaskService())
{
    if (!service.RootFolder.AllTasks.Any(t => t.Name == "YourScheduledTaskName"))
    {
        Console.WriteLine("YourScheduledTaskName is not installed on this system. Do you want to install it now? (y/n)");
        var answer = Console.ReadLine();
        if (answer == "y")
        {
            var task = service.NewTask();
            task.RegistrationInfo.Description = "YourScheduledTaskDescription";
            task.RegistrationInfo.Author = "YourAuthorName";

            var hourlyTrigger = new DailyTrigger { StartBoundary = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 1, 0 , 0) };
            hourlyTrigger.Repetition.Interval = TimeSpan.FromHours(1);
            task.Triggers.Add(hourlyTrigger);

            var taskExecutablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "YourScheduledTaskName.exe");
            task.Actions.Add(new ExecAction(taskExecutablePath));

            service.RootFolder.RegisterTaskDefinition("YourScheduledTaskName", task);
        }
    }
}
懷念過去 2024-08-06 14:12:39

顺便说一句,IRT 我接受的解决方案,这里是 CodeProject 包装器代码(请参阅 http://www .codeproject.com/KB/cs/tsnewlib.aspx )需要验证 ScheduledTask 是否存在

我在集成测试中使用它,因此断言是 NUnit..

public static void VerifyTask(string server, string scheduledTaskToFind)
{
     ScheduledTasks st = new ScheduledTasks(server);

     string[] taskNames = st.GetTaskNames();
     List<string> jobs = new List<string>(taskNames);

     Assert.IsTrue(jobs.Contains(scheduledTaskToFind), "unable to find " + scheduledTaskToFind);

     st.Dispose();
}

要检查它是否已启用,您可以执行以下操作:

Task task = st.OpenTask(scheduledTaskToFind);
Assert.IsTrue(task.Status != TaskStatus.Disabled);

btw, IRT my accepted solution, here is the CodeProject wrapper code (see http://www.codeproject.com/KB/cs/tsnewlib.aspx ) needed to verify that a scheduledTask exists

I use this in an Integration test so the Assert is NUnit..

public static void VerifyTask(string server, string scheduledTaskToFind)
{
     ScheduledTasks st = new ScheduledTasks(server);

     string[] taskNames = st.GetTaskNames();
     List<string> jobs = new List<string>(taskNames);

     Assert.IsTrue(jobs.Contains(scheduledTaskToFind), "unable to find " + scheduledTaskToFind);

     st.Dispose();
}

to check if it is enabled, you can do the following:

Task task = st.OpenTask(scheduledTaskToFind);
Assert.IsTrue(task.Status != TaskStatus.Disabled);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文