How to pass argument while calling Task Scheduler from c#

不想你离开。 提交于 2019-12-12 04:49:46

问题


Here I have create service for writing .txt file and passing parameter from Task scheduler using this code :

static void Main(string[] args)
    {

        string abc = string.Empty;
        foreach (var item in args)
        {
            abc += item +" ";
        }
        string path = @"D:\GST Project\Demo Text File.txt";
        File.WriteAllText(path, abc);
    }

I have added the task in task scheduler like this:

I want to call my scheduler task by using C# code below is my code which I have taken from Link

using (TaskService tasksrvc = new TaskService(server.Name, login, domain, password))
{
    Task task = tasksrvc.FindTask(taskName);
    task.Run();       
}

I am wondering here how we can pass the parameter through TaskService. Also What should I pass in place of Server.Name,login, domain,Password. Thanks for your help !


回答1:


try the below code:- This will help you

TaskService.Instance.AddTask("Test", QuickTriggerType.Daily, "Exe file path", "test");

this will schedule your task and pass the test argument in your exe file




回答2:


What should I pass in place of Server.Name,login, domain,Password

serverName - The name of the computer that you want to connect to. If the serverName parameter is empty, then this method will execute on the local computer.

login-The user name that is used during the connection to the computer. If the user is not specified, then the current token is used.

domain - The domain of the user specified in the user parameter.

password-The password that is used to connect to the computer. If the user name and password are not specified, then the current token is used

.

You can use a TaskDefinition to pass parmeters and other settings. Please try the following for passing the parameters

using (TaskService ts = new TaskService()) {

     TaskDefinition td = ts.NewTask();
     td.RegistrationInfo.Description = "Does something";

     //l fire the task at this time every day
     td.Triggers.Add(new DailyTrigger { DaysInterval = 1 });

     // Create an action that will launch Notepad and you can pass paremeters
     td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

     // Register the task in the root folder
     ts.RootFolder.RegisterTaskDefinition(@"Test", td);


  }


来源:https://stackoverflow.com/questions/45324638/how-to-pass-argument-while-calling-task-scheduler-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!