Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 742
emragins Avatar asked Apr 21 '11 22:04

emragins


People also ask

How can you tell if a process is completed in C#?

If it's a quick process, just wait for it. process. WaitForExit(); If you're starting one up in the background, subscribe to the Exited event after setting EnableRaisingEvents to true.

What is process start?

Start(ProcessStartInfo) Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.

How do I start a process in VB net?

Start another application using your . NET code As a . NET method, Start has a series of overloads, which are different sets of parameters that determine exactly what the method does. The overloads let you specify just about any set of parameters that you might want to pass to another process when it starts.


1 Answers

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.

like image 63
David Hall Avatar answered Sep 28 '22 03:09

David Hall