windows11的Task Scheduler
时间: 2024-01-24 11:04:50 浏览: 262
Windows 11的任务计划程序(Task Scheduler)是操作系统中的一个工具,用于安排和自动执行计算机上的各种任务。它允许用户创建、修改和删除计划任务,并可以指定任务的执行时间、频率和条件。
通过任务计划程序,用户可以安排计算机在特定时间或特定事件发生时执行某些操作。例如,可以设置计划任务来定期备份文件、运行病毒扫描、清理临时文件等。任务计划程序还可以在计算机启动时自动运行某些程序或脚本。
在Windows 11中,任务计划程序的界面和功能与之前的版本基本相同。用户可以通过以下步骤访问任务计划程序:
1. 打开“开始”菜单,并搜索“任务计划程序”。
2. 单击搜索结果中的“任务计划程序”应用程序,打开任务计划程序窗口。
在任务计划程序窗口中,用户可以创建新的计划任务、查看和修改已有的计划任务,并查看任务的运行历史记录和状态。
注意:以上是关于Windows 11的Task Scheduler的一般介绍,具体的操作步骤和功能可能会因操作系统版本和个人设置而有所不同。如果您有特定的问题或需求,请提供更多详细信息,以便我能够提供更准确的帮助。
相关问题
C# 用TaskScheduler.dll 创建 windows任务计划
使用 TaskScheduler.dll 可以很方便地创建、修改和删除 Windows 任务计划。以下是一个简单的 C# 示例,演示如何使用 TaskScheduler.dll 创建一个每天执行一次的任务计划:
```csharp
using System;
using System.IO;
using Microsoft.Win32.TaskScheduler;
class Program
{
static void Main(string[] args)
{
// 创建一个新的计划任务对象
using (TaskService taskService = new TaskService())
{
TaskDefinition taskDefinition = taskService.NewTask();
// 设置任务基本属性
taskDefinition.RegistrationInfo.Description = "My Daily Task";
taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
taskDefinition.Settings.Enabled = true;
// 创建触发器,每天执行一次
DailyTrigger dailyTrigger = new DailyTrigger();
dailyTrigger.Repetition.Interval = TimeSpan.FromDays(1);
dailyTrigger.StartBoundary = DateTime.Today.AddHours(9); // 每天 9 点执行
taskDefinition.Triggers.Add(dailyTrigger);
// 创建操作,将本地文件复制到远程位置
string sourceFile = @"C:\temp\test.txt";
string destinationPath = @"\\remote\share\test.txt";
CopyAction copyAction = new CopyAction(sourceFile, destinationPath, true);
taskDefinition.Actions.Add(copyAction);
// 注册任务计划
taskService.RootFolder.RegisterTaskDefinition("My Daily Task", taskDefinition);
}
}
}
```
在上面的示例中,我们创建了一个名为“My Daily Task”的任务计划,每天 9 点执行。操作是将本地文件复制到远程位置。你可以根据自己的需求修改代码。
C++ 用TaskScheduler.dll 创建 windows任务计划
可以使用Task Scheduler API来创建Windows任务计划。以下是一个简单的示例```c++
#include <windows.h>
#include <tasksd.h>
#include <comutil.h>
#pragma comment, "taskschd.lib")
#pragma comment(lib,comsuppw.lib")
int main()
{
CoInitialize);
ITaskService *pService = NULL HRESULT hr = CoCreateInstance(CLSID_Task, NULL, CLSCTX_INPROC_SERVER, IIDaskService, (void**)&pService);
if (FAILED(hr))
printf("Failed to create an instance of ITaskService: %x", hr);
return 1;
}
hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
printf("Failed to connect to Task Scheduler: %x", hr);
pService->Release();
return 1;
}
ITaskFolder *pRootFolder = NULL;
hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder);
if (FAILED(hr))
{
printf("Failed to get root folder: %x", hr);
pService->Release();
return 1;
}
ITaskDefinition *pTask = NULL;
hr = pService->NewTask(0, &pTask);
if (FAILED(hr))
{
printf("Failed to create a new task definition: %x", hr);
pRootFolder->Release();
pService->Release();
return 1;
}
IRegistrationInfo *pRegInfo = NULL;
hr = pTask->get_RegistrationInfo(&pRegInfo);
if (FAILED(hr))
{
printf("Failed to get registration info: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pRegInfo->put_Author(_bstr_t(L"Your Name"));
pRegInfo->Release();
if (FAILED(hr))
{
printf("Failed to set author: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
ITaskSettings *pSettings = NULL;
hr = pTask->get_Settings(&pSettings);
if (FAILED(hr))
{
printf("Failed to get task settings: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pSettings->put_StartWhenAvailable(VARIANT_TRUE);
pSettings->Release();
if (FAILED(hr))
{
printf("Failed to set start when available: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IPrincipal *pPrincipal = NULL;
hr = pTask->get_Principal(&pPrincipal);
if (FAILED(hr))
{
printf("Failed to get principal: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pPrincipal->put_RunLevel(TASK_RUNLEVEL_HIGHEST);
pPrincipal->Release();
if (FAILED(hr))
{
printf("Failed to set run level: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IActionCollection *pActions = NULL;
hr = pTask->get_Actions(&pActions);
if (FAILED(hr))
{
printf("Failed to get action collection: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IAction *pAction = NULL;
hr = pActions->Create(TASK_ACTION_EXEC, &pAction);
pActions->Release();
if (FAILED(hr))
{
printf("Failed to create action: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IExecAction *pExecAction = NULL;
hr = pAction->QueryInterface(IID_IExecAction, (void**)&pExecAction);
pAction->Release();
if (FAILED(hr))
{
printf("Failed to query interface for IExecAction: %x", hr);
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pExecAction->put_Path(_bstr_t(L"C:\\path\\to\\executable.exe"));
if (FAILED(hr))
{
printf("Failed to set executable path: %x", hr);
pExecAction->Release();
pTask->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
ITaskDefinition *pTaskDef = NULL;
hr = pTask->QueryInterface(IID_ITaskDefinition, (void**)&pTaskDef);
pTask->Release();
if (FAILED(hr))
{
printf("Failed to query interface for ITaskDefinition: %x", hr);
pRootFolder->Release();
pService->Release();
return 1;
}
ITriggerCollection *pTriggerCollection = NULL;
hr = pTaskDef->get_Triggers(&pTriggerCollection);
if (FAILED(hr))
{
printf("Failed to get trigger collection: %x", hr);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
ITrigger *pTrigger = NULL;
hr = pTriggerCollection->Create(TASK_TRIGGER_DAILY, &pTrigger);
pTriggerCollection->Release();
if (FAILED(hr))
{
printf("Failed to create trigger: %x", hr);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IDailyTrigger *pDailyTrigger = NULL;
hr = pTrigger->QueryInterface(IID_IDailyTrigger, (void**)&pDailyTrigger);
pTrigger->Release();
if (FAILED(hr))
{
printf("Failed to query interface for IDailyTrigger: %x", hr);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
hr = pDailyTrigger->put_Id(_bstr_t(L"DailyTrigger"));
if (FAILED(hr))
{
printf("Failed to set trigger ID: %x", hr);
pDailyTrigger->Release();
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
SYSTEMTIME st;
GetLocalTime(&st);
st.wHour = 12;
st.wMinute = 0;
st.wSecond = 0;
st.wMilliseconds = 0;
DATE start_date;
SystemTimeToVariantTime(&st, &start_date);
hr = pDailyTrigger->put_StartBoundary(_bstr_t(start_date));
pDailyTrigger->Release();
if (FAILED(hr))
{
printf("Failed to set start boundary: %x", hr);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
return 1;
}
IRegisteredTask *pRegisteredTask = NULL;
hr = pRootFolder->RegisterTaskDefinition(_bstr_t(L"MyTask"), pTaskDef, TASK_CREATE_OR_UPDATE, _variant_t(), _variant_t(), TASK_LOGON_INTERACTIVE_TOKEN, _variant_t(), &pRegisteredTask);
pTaskDef->Release();
pRootFolder->Release();
pService->Release();
if (FAILED(hr))
{
printf("Failed to register task definition: %x", hr);
return 1;
}
printf("Task successfully registered.");
pRegisteredTask->Release();
CoUninitialize();
return 0;
}
```
这个例子使用了COM和ATL,需要添加对comsuppw.lib和taskschd.lib库的链接。如果需要更改任务计划的设置,只需修改相应的属性即可。最后,调用ITaskFolder::RegisterTaskDefinition方法来注册任务计划。
阅读全文