如何知道 Process.Start() 是否成功? [英] How to know if Process.Start() is successful?

查看:30
本文介绍了如何知道 Process.Start() 是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了两种不同的方法来启动进程.

I've tried two different methods for starting a process.

第一个

定义被定义为 Start 方法的参数:

The definition is defined as parameters to the Start method:

System.Diagnostics.Process.Start("excel", string.Format(""{0}""", ExcelFileBox.Text.ToString()));

我的想法:

这个开始很好,但我不知道如何从中获得反馈.

This one starts just fine, but I don't know how to get feedback from it.

第二个

我开始研究 ProcessStartInfo 是因为我想知道 Excel 是否成功启动——例如,虽然它很可能存在于用户的机器上,但不能保证,这很愚蠢让我向用户表明它已成功启动,但尚未启动.

I started looking into ProcessStartInfo because I want to know if Excel started successfully or not--for instance, while it's very likely it exists on the user's machine, there's no guarantee and it would be silly for me to indicate to the user that it's started successfully when it hasn't.

System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo
{
    FileName = "excel",
    Arguments = string.Format(""{0}"", ExcelFileBox.Text.ToString()),
    ErrorDialog = true,
    UseShellExecute = false,
    WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};

try
{
    System.Diagnostics.Process.Start(startinfo);
}
catch (Exception err)
{
    Console.WriteLine(err.Message);
}

我的想法:

这给出了错误:系统找不到指定的文件",但不清楚是 Excel 应用程序还是我的文件.无论如何,虽然我很欣赏错误消息的能力,但我现在不应该收到.

This gives the error: "The system cannot find the file specified", but it's unclear whether it means the Excel application or my file. In any case, while I appreciate the error message ability, I shouldn't be receiving out at the moment.

关于如何知道这是否成功的想法、建议和想法?

Thought, suggestions, ideas on how to know if this happened successfully?

已解决

我将启动进程的第一种方法放入 try-catch 中,并且效果很好.

I put the first way of starting a process into a try-catch and it works beautifully.

推荐答案

Process.Start() 声明这个方法有一个 Boolean 类型的重载,返回值意味着:

The MSDN page on Process.Start() states that this method has an overload of type Boolean, where the return values mean:

如果进程资源已启动,则为真;如果没有新的进程资源,则为 false开始(例如,如果现有的过程被重用).

true if a process resource is started; false if no new process resource is started (for example, if an existing process is reused).

另外它可以抛出三个异常:

Additionally it can throw three exceptions:

  • 无效操作异常

未在 Process 组件的 StartInfo 中指定文件名.

No file name was specified in the Process component's StartInfo.

-或-

StartInfo 属性的 ProcessStartInfo.UseShellExecute 成员为 true,而 ProcessStartInfo.RedirectStandardInput、ProcessStartInfo.RedirectStandardOutput 或 ProcessStartInfo.RedirectStandardError 为 true.

The ProcessStartInfo.UseShellExecute member of the StartInfo property is true while ProcessStartInfo.RedirectStandardInput, ProcessStartInfo.RedirectStandardOutput, or ProcessStartInfo.RedirectStandardError is true.

  • Win32Exception

打开相关文件时出错.

  • ObjectDisposedException

进程对象已经被释放.

要使用 Process.Start() 的重载(这是该方法的唯一非静态重载),您需要使用 ProcessStartInfo 对象创建 Process 类的实例.

To use this overload of Process.Start() (which is the only non static overload of the method) you need to create an instance of the Process class using a ProcessStartInfo object.

一个例子如下:

ProcessStartInfo processStartInfo = new ProcessStartInfo("EXCEL.EXE");

Process process = new Process();
process.StartInfo = processStartInfo;
if (!process.Start())
{
    // That didn't work
}

不过,考虑到这仍然可能抛出,您最好在静态 .Start() 方法调用之一周围包裹一个捕获.

Though, given that this can still throw you are probably better of just wrapping a catch around one of the static .Start() method calls.

鉴于此,对 Process.Start() 的调用似乎很清楚,要么有效,要么无效,您可以通过返回值为 0(或抛出异常)来确定这一点.

Given that, it seems clear that the call to Process.Start() will either work or not and you can determine this from the return value being 0 (or an exception being thrown).

一旦您的流程开始,您就可以控制很多事情,使用 Process 类的属性,例如 HasExited,您可以检查流程处于什么状态.

Once your process has started you then have a lot of control over things, with properties of the Process class such as HasExited allowing you to check what state the process is in.

简而言之 - 如果用户在他们的机器上没有 excel,Process.Start() 将抛出异常.

In short - if the user does not have excel on their machine, Process.Start() will throw an exception.

这篇关于如何知道 Process.Start() 是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆