473,511 Members | 10,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add/Remove Windows Scheduled Tasks programatically through C#?

1 New Member
I am trying to schedule tasks in Windows from my C# program. The tasks get listed in Windows Schedule Tasks list, but it is not getting invoked. It seems I need to give the user name and password to start the task
OR
I have to set the property "RunOnlyIfLoggedOn" to true. But I don't know how to do this. Please help.

The following code I used is posted in one of the forums. Please find the code below.

--- CODE ---
simply use System.Diagnostics.Process.Start and issue an AT command.
Another option is to use the Management classes and the WMI class 'Win32_ScheduledJob'

Following a small sample to get you started:

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;
}
}

class JobScheduler {
public static void Main() {
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
{
Console.WriteLine("Wait for Job to be scheduled and Press: <Enter> to delete");
Console.ReadLine(); // For test purposes - Wait for job to be scheduled.
ret = ScheduleJob.Delete(JobID); // Get rid of this Job
}
Console.WriteLine(ret);
}
}

/* Days of week
Sunday 64,
Monday 1,
Tuesday 2,
Wednesday 4,
Thursday 8,
Friday 16,
Saturday 32

*/
Sep 9 '07 #1
0 8105

Sign in to post your reply or Sign up for a free account.

Similar topics

11
4050
by: Codemonkey | last post by:
Hi, I am writing an App in .Net that involves some scheduling of tasks. I was wondering if anybody has come accross any components or examples of how to implement a schedule manager like the one...
3
2160
by: timb | last post by:
Hi, I have created a windows service which is responsible for running certain tasks when they are scheduled to run. The service monitors a sql table and runs a task when the task scheduled time...
3
8664
by: vighnesh | last post by:
Hi Folks I am doing an VB.NET application in which I want to add scheduling feature, so that my application can run at a scheduled time, how can it be done programatically. Please post your...
0
4224
by: Satish | last post by:
Scheduled Tasks (.Net ) does not run when server is logged off (Windows 2003): I have a .net executable (command line exe) which performs certain business operations. I can run the program...
6
2771
by: arun.hallan | last post by:
Hi, I want to run a webservice method every night at a specified time, say 10pm. The current way i thought of doing this is to have a windows service and a timer which checks the time every...
3
3426
by: kj7ny | last post by:
How can I access and manipulate Scheduled Tasks in Windows using Python? I have a Windows XP workstation running Python 2.4.4 using the win32all modules to control the windows services on...
7
9092
by: whutto | last post by:
I have a VB.NET app that crashes when run from the Windows scheduled tasks, but runs fine when the run-as user is logged on. The app also runs fine from the scheduled jobs if the run as user is...
0
1961
by: Paulson | last post by:
Dear Freinds I want to make a program that acts as a reminder for the users.I need to open up the Scheduled task wizard programmatically.If you type Tasks in the run command the Tasks...
1
1624
by: naveensrirangam | last post by:
We want to do a total 10 number of scheduled tasks daily. These tasks will run in certain time in daily and every task application developed in different technologies . Now we are looking to do...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.