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

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

问题描述

我试图启动一个进程两种不同的方法。

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

第一

定义被定义为参数来启动方法:

The definition is defined as parameters to the Start method:

System.Diagnostics.Process.Start(创先争优,字符串。格式(\{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);
            }



我的想法:

My thoughts:

这给出了错误:系统找不到指定的文件,但目前还不清楚是否意味着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()的MSDN网页指出,这种方法有一个重载布尔类型,这里的返回值意味着:

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

true,如果一个进程的资源启动;
假如果没有新的进程的资源是
启动(例如,如果一个现有的
过程重复使用)。

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:


  • 出现InvalidOperationException

没有文件名是在Process组件的StartInfo的规定。

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

- 或 -

而ProcessStartInfo.RedirectStandardInput,ProcessStartInfo.RedirectStandardOutput或ProcessStartInfo.RedirectStandardError是真实的StartInfo的属性的ProcessStartInfo.UseShellExecute成员也是如此。

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


  • Win32Exception

有一个错误在打开相关的文件。

There was an error in opening the associated file.


  • 的ObjectDisposedException

工艺对象已经被释放。

The process object has already been disposed.

要使用的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.

这方面的一个例子如下:

An example of this is below:

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

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

虽然,因为这仍然可以抛出你可能会更好的包装只是静态的。开始()方法调用围绕着一个陷阱。

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).

在你的进程已经开始,那么你有很多在事情的控制,具有如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.

在短期 - 他们的机器上,如果用户不具有出类拔萃,的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天全站免登陆