2010-10-25 83 views

回答

1

您可以生成一个进程调用SCHTASKS.exe

下面是一些代码,我曾经做到这一点:

Process p = new Process(); 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.FileName = "SCHTASKS.exe"; 
p.StartInfo.RedirectStandardError = true; 
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 

p.StartInfo.Arguments = String.Format(
    "/Change /S {0} /TN {1} /RU {2}\\{3} /RP {4}", 
    MachineName, 
    ScheduledTaskName, 
    activeDirectoryDomainName, 
    userName, 
    password 
); 

p.Start(); 
// Read the error stream first and then wait. 
string error = p.StandardError.ReadToEnd(); 
p.WaitForExit(); 

if (!String.IsNullOrWhiteSpace(error)) 
{ 
    throw new Exception(error); 
} 
+0

你是什么意思由activeDirectoryDomainName? – xbonez 2010-10-26 13:47:01

+0

我首先试图在本地机器上实现相同的功能,所以我从参数列表中省略了MachineName,因此它拒绝了本地系统 – xbonez 2010-10-26 13:47:35

+1

activeDirectoryDomainName只是用于登录的Windows域。如果你是域的一部分,那么你的登录看起来像这样:'domain \ username'。否则,它看起来像这样:'username' – 2010-10-26 15:52:28