简体   繁体   中英

not able to create Event based Task Scheduler using c sharp

I tried to create window Task using c sharp. I made the code but the code is not working, I am getting runtime expcetionn. It is a event based Task scheduler. I am trying to schedule a Task which will invoke at the change of WIFI network in system.

I tried to check a lot of articles from Microsoft as well as outside also. I don want to use any third party libraries likes "Quartz" or so.

using (TaskService ts = new TaskService())
{
         //Create a new task definition and assign properties
         TaskDefinition td = ts.NewTask();

         td.RegistrationInfo.Description = "Git Config Details";

         // Create a trigger that will fire the task at this time every other day
         EventTrigger Etrigger = new EventTrigger("Mircosoft-Windows-NetworkProfile/Opertaional", "NetworkProfile", 10002);

                Etrigger.Enabled = true;

                td.Triggers.Add(Etrigger);

                // Create an action that will launch Notepad whenever the trigger fires
                td.Actions.Add(new ExecAction(@"C:\projects\Own\powershell\first.bat"));

                // Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition("Duplicate", td, TaskCreation.CreateOrUpdate, "NT AUTHORITY\\NETWORKSERVICE", null,
                                            TaskLogonType.ServiceAccount);
            }

my bad some spelling msitakes caused the issue. Microsoft and operational spelled wrong in my above code.

here is the new code for that which is in working condition now.

using (TaskService ts = new TaskService())
            {
                //Create a new task definition and assign properties
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "Duplicate";

                // Create a trigger that will fire the task at this time every other day
                EventTrigger Etrigger = new EventTrigger("Microsoft-Windows-NetworkProfile/Operational", "Microsoft-Windows-NetworkProfile", 10002);

                Etrigger.Enabled = true;

                td.Triggers.Add(Etrigger);

                // Create an action that will launch Notepad whenever the trigger fires
                td.Actions.Add(new ExecAction(@"C:\projects\Own\powershell\first.bat"));

                // Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition("Duplicate", td, TaskCreation.CreateOrUpdate, "NT AUTHORITY\\NETWORKSERVICE", null,
                                            TaskLogonType.ServiceAccount);
            }

Thanks to all folks commented on my post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM