Créer une task avec Task Scheduler Windows (depuis du code C#)
Bonjour tout le monde,
J'aimerais faire un petit programme qui s'interface avec le Task Scheduler de windows XP. C'est à dire, pouvoir créer une scheduled task depuis mon programme, et ensuite pouvoir la modifier (si besoin est) directement depuis le programme de windows.
J'ai trouvé ce code sur le net...mais il ne fonctionne pas.
Class:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
using System;
using System.Management;
using System.Reflection;
class ScheduleJob
{
public static uint Create(string Command, uint DaysOfMonth, uint DaysOfWeek,
bool InteractWithDesktop, bool RunRepeatedly, string StartTime, // in DMTF format !
out uint JobId)
{
// See: Platform SDK (or WMI SDK) doc's for detailed info about 'Win32_ScheduledJob' class
ManagementBaseObject inputArgs = null;
ManagementClass classObj = new ManagementClass(null, "Win32_ScheduledJob", null);
inputArgs = classObj.GetMethodParameters("Create");
inputArgs["Command"] = Command;
inputArgs["DaysOfMonth"] = DaysOfMonth;
inputArgs["DaysOfWeek"] = DaysOfWeek;
inputArgs["InteractWithDesktop"] = InteractWithDesktop;
inputArgs["RunRepeatedly"] = RunRepeatedly;
inputArgs["StartTime"] = StartTime;
// use late binding to invoke "Create" method on "Win32_ScheduledJob" WMI class
ManagementBaseObject outParams = classObj.InvokeMethod("Create", inputArgs, null);
JobId = ((uint)(outParams.Properties["JobId"].Value));
return ((uint)(outParams.Properties["ReturnValue"].Value));
}
// Delete the Scheduled (JobID)
public static uint Delete(uint JobID)
{
ManagementObject mo;
ManagementPath path = ManagementPath.DefaultPath;
path.RelativePath = "Win32_ScheduledJob.JobId=" + "\"" + JobID + "\"";
mo = new ManagementObject(path);
ManagementBaseObject inParams = null;
// use late binding to invoke "Delete" method on "Win32_ScheduledJob" WMI class
ManagementBaseObject outParams = mo.InvokeMethod("Delete", inParams, null);
return ((uint)(outParams.Properties["ReturnValue"].Value));
}
public static string ToDMTFTime(DateTime dateParam)
{
string tempString = dateParam.ToString("********HHmmss.ffffff");
TimeSpan tickOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dateParam);
tempString += (tickOffset.Ticks >= 0) ? '+' : '-';
tempString += (Math.Abs(tickOffset.Ticks) / System.TimeSpan.TicksPerMinute).ToString("d3");
return tempString;
}
} |
Main:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
uint JobID;
DateTime dt = DateTime.Now; // Get current DateTime
dt = dt.AddMinutes(1); //add 1 minute to current time
string LocalDateTime = ScheduleJob.ToDMTFTime(dt); // convert to DMTF format
// Schedule Notepad to run every Sunday and Wednesday
uint ret = ScheduleJob.Create(
// @"runas /user:administrator\domain /profile cmd ",
@"c:\winnt\notepad.exe",
0, 32, true, true, LocalDateTime, out JobID);
if (ret == 0) // sucess
{
System.Console.WriteLine("Wait for Job to be scheduled and Press: <Enter> to delete");
System.Console.ReadLine(); // For test purposes - Wait for job to be scheduled.
ret = ScheduleJob.Delete(JobID); // Get rid of this Job
}
System.Console.WriteLine(ret); |
Est-ce que quelqu'un l'aurai déjà fait, ou aurai une idée?
Merci de votre aide!