2
votes

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
what is the thrown exception :) ?? can you put it here - Kavindu Dodanduwa
is the exception "The system cannot find the file specified" ?? - Kavindu Dodanduwa
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
you don't have the fraps.exe app in the debug directory of your project - octavioccl

5 Answers

4
votes

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.

2
votes

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);
1
votes

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

1
votes

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

0
votes

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.