0

当您调用时:

ProcessStartInfo startInfo = new ProcessStartInfo("someExecutable.exe");

这实际上是运行someExecutable.exe还是只是监视它?我基本上是在看看这是否可以用来读取已经运行的 exe 的输出,或者它是否会调用 exe。

我正在尝试找到一种方法来监视和记录已经运行的 exe 中的一些值。

4

1 回答 1

0

这实际上是运行 someExecutable.exe 还是仅仅监视它而死?

答案既不是……也不是!关于 ProcessStartInfo的MSDN 文章指出:

您可以使用 ProcessStartInfo 类更好地控制您启动的进程。您至少必须手动或使用构造函数设置 FileName 属性。

并且

ProcessStartInfo 与 Process 组件一起使用。当您使用 Process 类启动进程时,除了附加到正在运行的进程时可用的信息之外,您还可以访问进程信息。

使用ProcessStartInfo类,您既不能启动也不能监视进程。

但是,根据您需要监控的内容(例如可执行文件的输出),您可以使用ProcessStartInfo类来重定向您将以编程方式启动的进程的输出。为此,您需要设置ProcessStartInfo类的RedirectStandardOutput 属性。因此,假设您将以编程方式启动进程,您需要该类以允许/配置进程监控。

注释 MSDN 示例以澄清我的答案:

// Process is NOT started yet! process name is set
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
// Process is NOT started yet! process object is initialized with the process info like filename and so on. 
Process.Start(startInfo);
// Process is NOT started yet! process arguments are set. 
// The equivalent command line in a shell would be: c:\>IExplore.exe www.northwindtraders.com ENTER is NOT pressed yet!
startInfo.Arguments = "www.northwindtraders.com";
// NOW the process is executed/started => 
// c:\>IExplore.exe www.northwindtraders.com <= ENTER is pressed, process is started/running!
// c:\>
Process.Start(startInfo); 
于 2014-01-08T19:41:14.713 回答