2012-10-31 26 views
3

我想創建一個實用程序來碎片整理我的網絡上的所有機器。我已經使用WMI的Defrag和DefragAnalysis方法取得了成功,但它們與Windows XP不兼容。這是一個問題,因爲我們在網絡上有一些XP機器。 我已經能夠在XP機器上本地調用defrag.exe進程來執行碎片整理,但是我在遠程機器上調用它時出現了問題。以下是我在本地工作的代碼,有人可以幫助我爲網絡中的遠程計算機做這項工作嗎?我嘗試過使用一些WMI來幫忙,但由於我對C#和WMI的新手感到失望,所以我沒有成功,謝謝!C#遠程運行defrag.exe

ProcessStartInfo info = new ProcessStartInfo(); 
info.FileName = "defrag"; 
info.Arguments = volume + " -f"; 
info.UseShellExecute = false; 
info.CreateNoWindow = true; 
info.RedirectStandardOutput = true; 

Process defrag = Process.Start(info); 
defrag.PriorityClass = ProcessPriorityClass.BelowNormal; 

while (!defrag.HasExited) 
{ 
    System.Threading.Thread.Sleep(1000); 
    Process[] procs = Process.GetProcessesByName("dfrgntfs"); 
    if (procs != null && procs.Length > 0) 
    { 
     procs[0].PriorityClass = ProcessPriorityClass.Idle; 
     defrag.WaitForExit(); 
    } 

    result = null; 
    while(!defrag.StandardOutput.EndOfStream) 
    { 
     //get output and store results 
    } 
+3

你可能會使用這種情況下工作的錯誤工具。嘗試使用'PSTools' http://technet.microsoft.com/en-au/sysinternals/bb897553 – Serdalis

回答

1

我可能只是使用PsExec遠程運行命令。這應該適用於任何Windows(NT)版本。

+0

好吧我已經下載了PsExec,並認爲我已經轉換爲使用此方法的代碼的第一部分,但我不知道該部分在我的while循環內(運行dfrgntf進程)。這是我到目前爲止... Process psexec = new Process(); psexec.StartInfo.FileName = @「C:\ PsExec.exe」; psexec.StartInfo.RedirectStandardOutput = true; psexec.StartInfo.UseShellExecute = false; psexec.StartInfo.CreateNoWindow = true; psexec.StartInfo.Arguments = @「\\ MyMachine C:\ WINDOWS \ system32 \ defrag.exe c:-f」; – mgrenier

+0

抱歉,我知道這不是最易讀的,但因爲我是新用戶,所以我現在無法回答自己的問題。 – mgrenier

+0

這條線似乎有問題... psexec.StartInfo.Arguments = @「\\ MyMachine defrag.exe c:-f」; 你在這裏看到問題嗎? – mgrenier

2

只是爲了完成這個主題,我想我會後,實際上爲我工作的代碼,這段代碼工作,你需要下載PsTools,並將其放置在根...

  Process psexec = new Process(); 

      psexec.StartInfo.FileName = @"C:\PsExec.exe"; 
      psexec.StartInfo.Arguments = @"-s \\" + machine + " defrag.exe " + volume + " -f"; 
      psexec.StartInfo.UseShellExecute = false; 
      psexec.StartInfo.CreateNoWindow = true; 
      psexec.StartInfo.RedirectStandardOutput = true; 

      psexec.Start(); 

      while (!psexec.HasExited) 
      { 
       System.Threading.Thread.Sleep(1000); 

       Process[] procs = Process.GetProcessesByName("dfrgntfs", @"\\" + machine); 
       if (procs != null && procs.Length > 0) 
       { 
        psexec.WaitForExit(); 
       } 

       while (!psexec.StandardOutput.EndOfStream) 
       { 
        //get output and store results 
       }