简体   繁体   English

无法使用c sharp创建基于事件的任务计划程序

[英]not able to create Event based Task Scheduler using c sharp

I tried to create window Task using c sharp. 我尝试使用c sharp创建窗口任务。 I made the code but the code is not working, I am getting runtime expcetionn. 我制作了代码,但代码不起作用,我得到运行时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. 我正在尝试安排一个任务,它将在系统中更改WIFI网络时调用。

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. 我不想使用像“Quartz”那样的任何第三方库。

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. 我的上述代码中的Microsoft和操作拼写错误。

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. 感谢所有人对我的帖子发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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