The Task Name doesn't like spaces in its name. Your code will run and not generate an exception, but the console windows closes so quickly you can't see it.
Well here's how I solved it.
I created a console application and added the following to the class:
public static readonly string Constans = "SCHTASKS.exe";
static void Main(string[] args)
{
// Runs daily at 8:00 AM
CreateScheduleTask("DAILY", "08:00:00", "LMCListenerSysTrayApp2010", @"C:\program files\lmclistener\LMCListenerSysTrayApp2010.exe");
// Runs Every minute
//CreateScheduleTask("MINUTE", 1, "LMCListenerSysTrayApp2010", @"C:\program files\lmclistener\LMCListenerSysTrayApp2010.exe"); // Note the Task name hate spaces in the name
}
public static bool CreateProcess(string strProcessName, string strCommandLineParams)
{
// set process parameters and invoke the process
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.WorkingDirectory = Directory.GetCurrentDirectory();
processInfo.Arguments = strCommandLineParams;
processInfo.FileName = strProcessName;
Process process = Process.Start(processInfo);
process.WaitForExit();
if (0 != process.ExitCode)
return false;
return true;
}
public static bool CreateScheduleTask(string strScheduleType, string intTimeInterval, string strTaskName, string strProgramPath)
{
StringBuilder commandLineParams = new StringBuilder();
commandLineParams.AppendFormat("/Create /RU SYSTEM /SC {0} /ST {1} /TN {2} /TR \"\\\"{3}\\\"", strScheduleType.ToUpper(), intTimeInterval, strTaskName, strProgramPath);
return CreateProcess(Constans, commandLineParams.ToString());
}
No comments:
Post a Comment