Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
2.14/5 (3 votes)
I am new to programming and I have found a code to register a task under root folder in task scheduler.
I have the below code in which there is a method "eTrigger.SetBasic("Security", "Microsoft Windows security auditing.", 4625)" what it does is it creates a duplicate "Log Name" and "Log Source" and bottom line my code does not work.
I want to use eTrigger.GetBasic() or eTrigger.Subscription to address my code to the actual event logs. When I use eTRigger.GetBasic() it with the arguments I am giving in eTRigger.SetBasic("Security", "Microsoft Windows security auditing.", 4625) it gives me error

Can someone fix this code for me?

C#
class Program
{
    static void Main(string[] args)
    {

        using (TaskService ts = new TaskService())
        {
            // Create a new task definition and assign properties
            TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Description = "Does something";
            // Create a trigger that will fire the task at this time every other day
            // whether user is logged on or not
            EventTrigger eTrigger = (EventTrigger)td.Triggers.Add(new EventTrigger());
            EventLog securityLog = new EventLog("Security", System.Environment.MachineName);
            //this is where I see problem. I want to use eTrigger.GetBasic
            eTrigger.SetBasic("Security", "Microsoft Windows security auditing.", 4625);
            eTrigger.Enabled = true;
            eTrigger.ExecutionTimeLimit = TimeSpan.Zero;
            // Create an action that will launch Notepad whenever the trigger fires
            td.Actions.Add(new ExecAction(@"C:\Windows\notepad.exe"));
            // Register the task in the root folder
            ts.RootFolder.RegisterTaskDefinition("test", td);
        }
    }
}
Posted
Updated 26-Apr-14 17:35pm
v2
Comments
DamithSL 26-Apr-14 23:54pm    
what is the error you get?
Curtis Hagen 27-Apr-14 0:11am    
This method has invalid arguments . Arguments must be passed with out keyword. What I think if I am able to give them a path to Event Log and Event Source it would do the trick. But I don't know what path to give
DamithSL 27-Apr-14 0:22am    
are you using Microsoft.Win32.TaskScheduler?
Curtis Hagen 27-Apr-14 0:24am    
Indeed I am
Curtis Hagen 27-Apr-14 0:29am    
Infact I have tried to use the below but it didn't work also.

EventLog securityLog = new EventLog("Security", System.Environment.MachineName);
securityLog.Source = "Microsoft Windows security auditing.";
securityLog.Log="Security";
eTrigger.GetBasic(securityLog.Log,securityLog.Source,4625)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900