C# equivalent of cd C:\appFolder c:\app.exe?

1.4k Views Asked by At

Possible Duplicate:
.Net Process.Start default directory?

I have a C# application, mono to be specific. This application needs to launch another application on the users system. I know of Process.Start and how to use it, but theres something pecuilar about this instance which makes that not work correctly.

For some reason the program I am trying to launch via Process.Start needs to be called from the directory it resides in, otherwise it gives an error on opening.

What I mean by that is, if I open up a command prompt and type in: C:\appFolder\app.exe The application will then give me an error.

However if I open a prompt and go: cd c:\appFolder app.exe

It then launches just fine.

The problem I am having with process.start is it tries to open the application without first doing what is the equivalent of 'cd c:\appFolder', and so the application gives an error on opening.

So how can I do make Process.Start do what would be the equivalent of first navigating to the apps folder 'cd c:\appFolder' and then calling app.exe?

BTW, I have solved this problem by putting cd C:\appFolder app.exe

into a .bat file, and have Process.Start open the .bat file, which works just fine. But I am curious to know if there is a way to cut out the .bat file.

4

There are 4 best solutions below

0
On BEST ANSWER

Using cd blah just changes your working directory. You can set the working directory of your process by setting the WorkingDirectory of your ProcessStartInfo. Perhaps something like this:

var procInfo = new ProcessStartInfo("app.exe");
procInfo.WorkingDirectory = @"C:\appFolder";
Process.Start(procInfo);
1
On

try changing the working directory before your call

Directory.SetCurrentDirectory(@"path");

http://msdn.microsoft.com/en-us/library/system.io.directory.setcurrentdirectory.aspx

0
On

Use a ProcessStartInfo object to start the application and set the WorkingDirectory property accordingly.

0
On
        var psi = new ProcessStartInfo("app.exe");
        psi.WorkingDirectory = @"C:\appFolder";
        Process.Start(psi);