1

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.

Community
  • 1
  • 1
eem
  • 53
  • 5

4 Answers4

3

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);
vcsjones
  • 138,677
  • 31
  • 291
  • 286
1

try changing the working directory before your call

Directory.SetCurrentDirectory(@"path");

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

juergen d
  • 201,996
  • 37
  • 293
  • 362
  • 2
    A possible downside to this approach is that it will change the working directory for the *current* program as well; not just the spawned process. – vcsjones Jan 19 '12 at 18:52
1
        var psi = new ProcessStartInfo("app.exe");
        psi.WorkingDirectory = @"C:\appFolder";
        Process.Start(psi);
D-Mac
  • 92
  • 8
0

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

Icarus
  • 63,293
  • 14
  • 100
  • 115