2010-10-27 31 views
5

我需要將在某個主機上運行的所有計劃任務列表轉換爲C#中的列表或數組。如何通過Process.Start()以列表或數組的形式讀取從命令運行的標準輸出

查詢

schtasks /query /S CHESTNUT105B /FO List 

返回像這樣的列表:

HostName:  CHESTNUT105B 
TaskName:  Calculator 
Next Run Time: 12:00:00, 10/28/2010 
Status:  Running 

HostName:  CHESTNUT105B 
TaskName:  GoogleUpdateTaskMachineCore 
Next Run Time: At logon time 
Status: 

HostName:  CHESTNUT105B 
TaskName:  GoogleUpdateTaskMachineCore 
Next Run Time: 13:02:00, 10/28/2010 

我有以下的代碼來執行我上面指定的命令:

static void Main(string[] args) 
{ 
    Process p = new Process(); 
    p.StartInfo.UseShellExecute = false; 
    p.StartInfo.FileName = "SCHTASKS.exe"; 
    p.StartInfo.RedirectStandardError = true; 
    p.StartInfo.RedirectStandardOutput = true; 
    p.StartInfo.CreateNoWindow = true; 
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 


    string MachineName = "CHESTNUT105b"; 
    string ScheduledTaskName = "Calculator"; 
    string activeDirectoryDomainName = "xxx"; 
    string userName = "xxx"; 
    string password = "xxxxx"; 

    p.StartInfo.Arguments = String.Format("/Query /S {0} /FO LIST", MachineName); 

    p.Start(); 
} 

我怎麼能讀列表生成到C#中的列表?

這個問題的
+0

我確信有更好的方法來檢索列表 - 通過WMI類,但我不知道該名稱空間足以提供答案。 – Oded 2010-10-27 19:03:12

回答

3

像這樣的東西應該工作(未經測試)。這將在列表的一個元素中包含每行輸出。現在

class GetSchTasks { 

    List<string> output = new List<string>(); 

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


     string MachineName = "CHESTNUT105b"; 
     string ScheduledTaskName = "Calculator"; 
     string activeDirectoryDomainName = "xxx"; 
     string userName = "xxx"; 
     string password = "xxxxx"; 

     p.StartInfo.Arguments = String.Format("/Query /S {0} /FO LIST", MachineName); 

     p.Start(); 
     p.BeginOutputReadLine(); 
     p.BeginErrorReadLine(); 
     p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); 
     p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); 
     p.WaitForExit(); 
     p.Close(); 
     p.Dispose(); 

    } 

    void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     //Handle errors here 
    } 

    void p_OutputDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     output.Add(e.Data); 
    } 

} 

,你可以解釋該列表之後建立一組合適的代表每一個計劃任務,或者不根據實際使用情況的對象。您也可以在p_OutputDataReceived處理程序本身中構建一個ScheduledTask的對象列表,只需比較每行與預期的開頭,例如,if (e.Data.StartsWith("HostName:")) { //parse the line and grab the host name }

+0

如果沒有事件處理程序,有沒有辦法做到這一點? – xbonez 2010-10-27 19:37:39

+0

是的,將p.StandardOutput附加到StreamReader,請參閱問題@davisoa鏈接(http://stackoverflow.com/questions/902116/c-how-to-read-console-output-with-parameters) – 2010-10-27 19:53:04

+0

讓它工作。謝謝 – xbonez 2010-10-27 20:32:21

1

部分可以通過此以前question回答 - 「C#如何閱讀控制檯輸出隨着參數」

檢索控制檯輸出到一個StreamReader後在這個問題的建議,那麼你只需要解析控制檯輸出到單獨的計劃任務,然後到一個對象,存儲您感興趣的每一塊數據。

要分解成單個任務,它看起來像你可以使用:str.split("\n\n") - 這會給你每個任務作爲一個單獨的字符串,循環遍歷這個數組,並創建一個讀取字符串並通過解析數據填充其值的類。

相關問題