如何在从 C# 调用任务计划程序时传递参数

How to pass argument while calling Task Scheduler from c#

这里我创建了用于编写 .txt 文件并使用以下代码从 Task scheduler 传递参数的服务:

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

我在任务调度程序中添加了这样的任务:

我想使用 C# 代码调用我的调度程序任务,下面是我从 Link

中获取的代码
using (TaskService tasksrvc = new TaskService(server.Name, login, domain, password))
{
    Task task = tasksrvc.FindTask(taskName);
    task.Run();       
}

我想知道如何通过 TaskService 传递参数。还 我应该传递什么来代替 Server.Name、登录名、域、密码。 感谢您的帮助!

试试下面的代码:- 这会帮助你

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

这将安排您的任务并在您的 exe 文件中传递 test 参数

我应该传递什么来代替 Server.Name、登录名、域、密码

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

.

您可以使用 TaskDefinition 来传递参数和其他设置。请尝试以下方法传递参数

使用 (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);


  }