开发者

c# calling process "cannot find the file specified"

开发者 https://www.devze.com 2022-12-31 19:41 出处:网络
I\'m a c# newbie so bear with me. I\'m trying to call \"pslist\" from PsTools from a c# app, but I keep getting \"The system cannot find the file specified\". I thought I read somewhere on google that

I'm a c# newbie so bear with me. I'm trying to call "pslist" from PsTools from a c# app, but I keep getting "The system cannot find the file specified". I thought I read somewhere on google that the exe should be in c:\windows\system32, so I tried that, still nothing. Even trying the full path to c:\windows\system32\PsList.exe is not working. I can open other things like notepad or regedit. Any ideas?

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 o开发者_开发问答f 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();
    }


You shouldn't do this in the first place unless you really need to.

Instead, use the Process class.
You can get all of the currently executing processes by calling Process.GetProcesses

To get the memory use of a single process, check the WorkingSet64 property of the Process object.
To get the CPU usage of a single process, use Performance Counters.

0

精彩评论

暂无评论...
验证码 换一张
取 消