2010-05-25 85 views
0

我是一個C#新手,所以忍受着我。我試圖從一個C#應用程序中從PsTools調用「pslist」,但我一直在收到「系統找不到指定的文件」。我以爲我讀過谷歌的某個地方,該exe文件應該在c:\ windows \ system32中,所以我試過了,仍然沒有。即使嘗試完整路徑到C:\ WINDOWS \ SYSTEM32 \ PsList.exe不起作用。我可以打開其他東西,如記事本或註冊表。有任何想法嗎?c#調用進程「無法找到指定的文件」

C:\WINDOWS\system32>dir C:\WINDOWS\SYSTEM32\PsList.exe 
Volume in drive C has no label. 
Volume Serial Number is ECC0-70AA 

Directory of C:\WINDOWS\SYSTEM32 

04/27/2010 11:04 AM   231,288 PsList.exe 
       1 File(s)  231,288 bytes 
       0 Dir(s) 8,425,492,480 bytes free
try 
    { 
     // Start the child process. 
     Process p = new Process(); 
     // Redirect the output stream of the child process. 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 

     //This works 
     //p.StartInfo.FileName = @"C:\WINDOWS\regedit.EXE"; 

     //This doesn't 
     p.StartInfo.FileName = @"C:\WINDOWS\system32\PsList.exe"; 
     p.Start(); 

     // Do not wait for the child process to exit before 
     // reading to the end of its redirected stream. 
     p.WaitForExit(); 
     // Read the output stream first and then wait. 
     s1 = p.StandardOutput.ReadToEnd(); 
     p.WaitForExit(); 

    } 
    catch (Exception ex) 
    { 
     Console.WriteLine("Exception Occurred :{0},{1}", 
     ex.Message, ex.StackTrace.ToString()); 
     Console.ReadLine(); 
    } 
+0

您是否在管理員帳戶下運行了Visual Studio?這可能是用戶權限發生的事情。您也可以在Windows上禁用UAC以進行測試。 – 2010-05-25 16:34:37

+1

@Junior:UAC不能導致FileNotFound。 – SLaks 2010-05-25 16:39:46

+0

您是否正在運行64位Windows? – SLaks 2010-05-25 16:40:22

回答

1

首先你不應該這樣做,除非你真的需要。使用Process class代替。
你可以通過調用Process.GetProcesses

獲得所有當前正在執行的過程中要獲得內存使用一個單一的過程中,檢查Process對象的WorkingSet64 property
獲取單個進程的CPU使用率,use Performance Counters

+0

Thanx快速響應! 是的,我正在運行64位窗口(Windows Server 2003) 我想用一些參數Pslist.exe的輸出獲取一些CPU /內存信息。但第一步就是讓PsList.exe執行。 我現在使用PsList.exe的原始位置(不在system32中)的完整路徑,它突然工作。 有沒有辦法讓應用程序在PATH變量中查找,所以我不必指定完整路徑? – laura 2010-05-25 16:44:27

+0

您應該改用.Net庫。它會更快,更簡單,更可靠。調用'Process.GetProcesses',然後遍歷'Process'對象。對於CPU使用率,請使用[性能計數器](http://blogs.msdn.com/b/bclteam/archive/2006/06/06/619284.aspx)。 – SLaks 2010-05-25 16:48:00

+0

@laura:我不知道windows 2003的行爲如何,但是在Vista上,取決於你想要用Process類做什麼(就像訪問模塊一樣),你可能會遇到權限問題。你可以嘗試它作爲Slaks說:foreach(在Process.GetProcesses()中的Process p){/ *做一些p變量* /} – 2010-05-25 16:53:19

相關問題