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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…