tags:

views:

62

answers:

6

I have an app launching an executable which is in the same folder as that app, by doing:

            Process procStarter = new Process();
            procStarter.StartInfo.FileName = "OtherApp.exe";
            procStart.Start();

which works fine, until I used a file open or file save dialog in my app. Then it can't find OtherApp.exe.

Does that seem normal? Can I just fix it by adding the current folder to StartInfo.Filename (and how do I obtain the current folder)?

A: 

Try explicitly specifying the path to OtherApp.exe.

Your open file dialog may be changing the current directory.

Eric J.
A: 

If you don't specify the folder explicitly, the system will look in the "current working directory" for the process.

The current working directory (usually) starts off as the application exe directory, but can be changed by browsing to with an Open or Save dialog.

Using an explicit filepath is the right answer. Best way is to not rely on the working directory at all, but to use the filepath of the current executable as a base.

Here are some ways to do this: Application.StartupPath, Application.ExecutablePath

Code might look something like this ...

var exeName = "sample.exe";
var exePath
    = Path.Combine(
        Path.GetDirectoryName( Application.ExecutablePath ),
        exeName);
Bevan
+8  A: 

Using the file dialog probably changes the current directory of your process. To access a file in the same folder as your current executable you can use the following code:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, "OtherApp.exe");
0xA3
+2  A: 

Or you could add to your code:

saveFileDialog1.RestoreDirectory = true ;

when prompting for filenames.

Carlos Gutiérrez
+1  A: 

The issue is that you can possibly change the current working directory when doing other file operations.

You want to remember the path as the other posters have showed you, but it may be that you want to create your own ProcessStartInfo instance and use the ProcessStartInfo.WorkingDirectory property so that you can remember the correct path.

Digicoder
A: 

Try System.IO.Path.Combine( System.Windows.Forms.Application.StartupPath, "myfile.exe" );

If it's not a winforms project divo's answer is best (imo, at the time of this answer)

Davy8