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

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

问题描述



第一个



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



System.Diagnostics.Process.Start(excel,string。格式(\{0} \,ExcelFileBox.Text.ToString()));



我的想法: / p>

这一个启动很好,但我不知道如何从中获得反馈。



第二个



我开始研究 ProcessStartInfo因为我想知道Excel是否成功启动 - 例如,尽管它很可能存在于用户的机器上,但是不能保证,对于用户来说,这将是愚蠢的它没有成功启动。

  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(异常错误)
{
Console.WriteLine(err.Message);
}

我的想法:



这会给出错误:系统找不到指定的文件,但是不清楚它是指Excel应用程序还是我的文件。在任何情况下,虽然我感谢错误信息的能力,我不应该现在收到。



想法,建议,想法如何知道这是否发生成功?



解决



我把第一种启动流程的方法

解决方案

MSDN页面 Process.Start()指出这个方法有一个Boolean类型的重载,返回值意味着:


如果进程资源启动,则为true;
如果没有新的进程资源是
启动(例如,如果现有的
进程被重用),则为false。


此外,它可以抛出三个例外:




  • InvalidOperationException



在Process组件的StartInfo中没有指定文件名。



-or-



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




  • Win32Exception



打开相关文件。




  • ObjectDisposedException



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



一个例子如下:

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

Process process = new Process();
process.StartInfo = processStartInfo;
if(!process.Start())
{
//没有工作
}

尽管如此,由于这仍然可以抛出您可能更好的只是围绕一个静态.Start()方法调用来捕获。






鉴于此,似乎很清楚,对Process.Start()的调用将会工作,也可以从返回值为0(或抛出一个异常)。



一旦你的进程开始,你就可以对事物进行很多的控制,Process类的属性如HasExited允许你检查



简而言之,如果用户在其机器上没有excel,那么Process.Start()将抛出异常。


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

The first

The definition is defined as parameters to the Start method:

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

My thoughts:

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

The second

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:

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?

Solved

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

解决方案

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

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

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

-or-

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.


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.


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

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.

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

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

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