2

I'm making a program that launches a program when a button is clicked. It seems to work for programs such as Chrome or Word but not smaller programs such as fraps and gives me the error

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll Additional information: The system cannot find the file specified

private void Fraps_Click(object sender, EventArgs e)
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "fraps.exe";
    Process.Start(startInfo);
}
5
  • What is the actual message of the exception? I bet it has something to do with "I don't know where 'fraps.exe' is".
    – gunr2171
    Dec 22, 2014 at 3:31
  • 1
    what is the thrown exception :) ?? can you put it here Dec 22, 2014 at 3:31
  • is the exception "The system cannot find the file specified" ?? Dec 22, 2014 at 3:35
  • it seems it cant find the file here is the full message: An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll Additional information: The system cannot find the file specified
    – Woot
    Dec 22, 2014 at 3:37
  • you don't have the fraps.exe app in the debug directory of your project
    – ocuenca
    Dec 22, 2014 at 3:39

5 Answers 5

4

Windows does not know what "fraps.exe" is, and it can't find such an executable file in the folders it is told to search in.

Let's verify this. Open a command prompt and type in "fraps.exe". It should not matter where your current directory is; the home folder should do nicely.

enter image description here

As long as you have not installed fraps to your home folder, this is what you should see. Now, why would this work for "chrome.exe" (presumably, not the case on my system) but not fraps?

Behind the scenes, Windows is told to look in a number of folders when you type commands. You can see the full listing if you type in SET into the prompt, and search for PATH.

enter image description here

That's a bit of a mess, but it's where Windows is going to look for the program you want after looking in the current directory.

In this case, Windows can't find "fraps.exe" in the current directory (your bin\Debug folder) or any of the folders in PATH. So it throws its hands up and says The system cannot find the file specified.

You have two options to fix this:

One: use the full path name. If you know where fraps is installed, you can use the full path name of the executable instead, such as:

startInfo.FileName = @"c:\Program Files\Fraps\fraps.exe";

Two: add the fraps install folder to the PATH environment variable. This more tricky and complicated, but you more or less append the path that fraps in installed to into the PATH variable. You can either do this, or rely on the end-user to do this (though that does not sound like a good idea to me).

Both options require that you know where fraps is installed to, so it's a good idea to hardcode the value (if this program is only for yourself) or ask the user for the install path when starting/installing the program.

1
  • Thanks for the explanation that clears some things up the full path works perfectly.
    – Woot
    Dec 22, 2014 at 3:48
2

Quoting from MSDN:

You must set at least the FileName property before you start the process. The file name is any application or document. A document is defined to be any file type that has an open or default action associated with it. You can view registered file types and their associated applications for your computer by using the Folder Options dialog box, which is available through the operating system. The Advanced button leads to a dialog box that shows whether there is an open action associated with a specific registered file type.

In your case fraps.exe is not a registered file type associated application. So give FULL PATH to fraps.exe

The Code Example would be like the following (Find fraps installation on your hdd and replace path to fraps.exe in second line)

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Program Files\fraps\fraps.exe"; // Your absolute PATH 
Process.Start(startInfo);
3
  • Ahh I see that works, but is there a way so that if fraps.exe is in a different directory it would detect where it is so if it was installed somewhere else the application would still work?
    – Woot
    Dec 22, 2014 at 3:46
  • you will need to set PATH I guess :) Dec 22, 2014 at 3:48
  • It has nothing to do with having a file type registered, and everything to do with not being in the PATH environment variable/current directory; you should probably update the answer accordingly. Dec 22, 2014 at 4:01
2

Was having the same problem from Process.Start(). I had an EXE whos solution platform was targeting "Any CPU".
My app was installed on a 64 bit CPU running EXCEL (also 64 bit).

The solution was to target an x64 build. (A.Kadir Bener eluded to this)

To change the Solution Platform Right Click the solution in Solution Explorer...scroll down to Configuration Manager...
Under Active solution platform: select x64 (if an option)
If x64 is not an option then select New...
Under Type or select the new platform...select x64

1

If the exe which you will call is 32bit then your code must be 32bit also. AnyCpu or 64bit gives that error.

2
  • This helped me.
    – pabben
    Feb 28, 2019 at 17:30
  • Yes...make the app platforms (x86,x64) match. Jun 29, 2019 at 23:04
0

IMHO, you can call any program that has a path entry in environment. Here, we are not sure if the exe exists in the bin directory of the executing application. Could you have tried with the full path, you would not have got the exception

I would suggest that you can bootstrap the exe as part of your app, since its not part of windows or you could hint the user to make sure path is in environment variables.

1
  • 1
    Sorry I'm not very good with visual studio how would I bootstrap the exe to my code?
    – Woot
    Dec 22, 2014 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.