Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

c# - How to know if Process.Start() is successful?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...