0

嗨,我有一个计划任务的程序。虽然这在 Windows7 机器上运行良好,但它在 XP 上引发错误。

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaskScheduler;

namespace MyTaskScheduler
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string STR_DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                string taskName = "MyTask";
                string taskFolder = "MyTaskFolder";

                Console.WriteLine("Creating task");
                ITaskService taskScheduler = new TaskSchedulerClass();
                taskScheduler.Connect(null, null, null, null);

                ITaskFolder rootFolder = taskScheduler.GetFolder(@"\");
                bool _found = false;
                foreach (ITaskFolder fol in rootFolder.GetFolders(0))
                {
                    if (fol.Name == taskFolder)
                        _found = true;
                }
                if (!_found)
                    rootFolder.CreateFolder(taskFolder, null);
                ITaskFolder folder = rootFolder.GetFolder("\\" + taskFolder);

                _found = false;
                foreach (IRegisteredTask tsk in folder.GetTasks(0))
                {
                    if (tsk.Name == taskName)
                        _found = true;
                }  
                if (_found)
                    folder.DeleteTask(taskName, 0);

                Console.WriteLine("Creating new task");
                ITaskDefinition taskDef = taskScheduler.NewTask(0);
                taskDef.RegistrationInfo.Description = "Opens notepad at the scheduled times";
                taskDef.RegistrationInfo.Author = "Monica";
                taskDef.Settings.Enabled = true;
                taskDef.Settings.Hidden = false;
                taskDef.Settings.DisallowStartIfOnBatteries = false;
                taskDef.Settings.WakeToRun = true;

                IWeeklyTrigger trigger = (IWeeklyTrigger)taskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_WEEKLY);
                //if (intWeekDays != 0)
                trigger.DaysOfWeek = (short)10;
                trigger.Id = "MyTimeSet";
                trigger.StartBoundary = DateTime.Now.Date.AddHours(10).AddMinutes(15).AddSeconds(20).ToString(STR_DateTimeFormat.Replace(" ", "T"));

                IExecAction action = (IExecAction)taskDef.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
                action.Id = "MyRun";

                string actionFile = "notepad.exe";

                action.Path = String.Concat("\"", actionFile, "\"");

                action.Arguments = String.Format("argus \"{0}\"", "argument1");

                IRegisteredTask regTask = null;

                regTask = folder.RegisterTaskDefinition(taskName, taskDef, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_NONE, "");

                Console.WriteLine("task created");                
            }
            catch (Exception e1)
            {
                Console.WriteLine(e1.ToString());
            }
        }
    }
}

我在 XP 机器上运行这个程序时遇到的错误是:

System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class     factory for component with CLSID {0F87369F-A4E5-4CFC-BD3E-73E6154572DD} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).   at TaskScheduler.Program.Main(String[] args)

有人可以告诉我这里哪里出错了。

谢谢,莫妮卡

4

1 回答 1

3

此代码使用的任务计划程序 2.0界面仅从 Windows Vista 开始可用。此代码将永远无法在 Windows XP 上运行。

一个后备选项是使用Task Scheduler 1.0接口,但由于可用功能存在很大差异,这可能还不够。在这种情况下,您无能为力,因为操作系统根本不提供您需要的服务。

于 2013-03-27T22:57:54.600 回答