2

I am trying to call Process.Start() on an executable. If the file cannot be found, it should be copied into the required location and then try again.

According to the documentation, Process.Start() can throw a FileNotFoundException when The file specified in the startInfo parameter's FileName property could not be found.

Based on that, the following would seem like a reasonable approach:

try
{
    Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
}
catch (FileNotFoundException ex)
{
    File.Copy(@"Z:\Unused\Apps\IT Support App\IT Self Help.exe", @"C:\users\Angus.McAngerson\desktop");
    Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe", "vdi");
}

However, the Start() in the try block only ever throws a Win32Exception:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

Message: The system cannot find the file specified

ErrorCode: -2147467259

NativeErrorCode: 2

I tried changing the try code to:

var procsi = new ProcessStartInfo(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
Process.Start(procsi);

But with the same results. I also tried changing the BuildPlatform to x86, x6 and Any CPU but without any difference.

Why is this happening? How can I throw a FileNotFoundException?


Update

The documentation states:

FileNotFoundException:

The file specified in the startInfo parameter's FileName property could not be found.

In the case above, the file cannot be found, yet the code throws a different exception. That is at least very misleading if not completely untrue.

The only explanation I can think of is that the program tries to run the file without checking whether it exists. This is fair enough, but then in what scenario would FileNotFoundException ever even happen?

Is this an error in the documentation?

Community
  • 1
  • 1
Bassie
  • 9,529
  • 8
  • 68
  • 159

3 Answers3

3

If you look here: https://msdn.microsoft.com/en-us/library/system.componentmodel.win32exception(v=vs.110).aspx

It will show you that you'll get the Win32Exception when trying to open an executable that doesn't exist.

So actually, your code seems to be functioning fine.

But if you really want a FileNotFoundException, then you're gonna have to do some checks yourself before running the process and throw the exception yourself.

if(!File.Exists(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe"))
{
     throw new FileNotFoundException("This file was not found.");
}

Edit

It does seem as though there may be an error in the documentation as I can't get it to throw the FileNotFoundException either, even though it claims to be able to.

So, you can either handle the Win32Exception or do what I suggested above.

Maybe someone else can shed light on this?

ThePerplexedOne
  • 2,920
  • 15
  • 30
  • I think I reported it to Microsoft (at the bottom of the page) but I have no idea if that has any impact! – Bassie Sep 14 '16 at 10:25
3

It's really not a good idea to execute statements in catch unless they are related to exception tracking.

I dont see why you cant use File.Exists (since you intend to do nothing with FileNotFoundException ). If we rewrite your code then it would be:

if (File.Exists(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe"))
{
    Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
}
else
{
    File.Copy(@"Z:\Unused\Apps\IT Support App\IT Self Help.exe", @"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
    Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe", "vdi");
}
Bassie
  • 9,529
  • 8
  • 68
  • 159
Rohit
  • 10,056
  • 7
  • 50
  • 82
  • I do not "intend to do nothing with FileNotFoundException" - the question is about how/why the exceptions are thrown as they are – Bassie Sep 14 '16 at 10:30
  • 1
    I thought its about right coding practices..sorry my bad – Rohit Sep 14 '16 at 10:35
1

If you have a look at the documentation for Process.Start you will notice that there is a table of exception conditions including:

Win32Exception - An error occurred when opening the associated file.

FileNotFoundException - The PATH environment variable has a string containing quotes.

Looks like it's behaving correctly to me even if the exceptions are a little misleading

Scott Perham
  • 2,410
  • 1
  • 10
  • 20
  • 1
    Seems you are correct if you follow that link for `Process.Start(string)`, but if you check my link `Process.Start(StartInfo)` then it doesn't mention anything of the sort - very misleading! – Bassie Sep 14 '16 at 10:04
  • Actually... it does... (the table actually contains _a lot_ more information...) still just as misleading though :) https://msdn.microsoft.com/en-us/library/0w4h05yb(v=vs.110).aspx – Scott Perham Sep 14 '16 at 10:06
  • It literally says: `FileNotFoundException - The file specified in the startInfo parameter's FileName property could not be found.` Isn't that exactly what is happening in my scenario? Does the program try to run the file before checking if it exists? – Bassie Sep 14 '16 at 10:08
  • Ha! Yes you're right :P I would suggest filing that as a documentation bug as it's clearly not true – Scott Perham Sep 14 '16 at 10:10