Sasquatch
Sasquatch

Reputation: 13

C# Run another program without copying .exe file

I have a program that runs another extern program by using the Process.Start()-Method with the full path (like "C:\some\folders\here\externprogram.exe").

The problem I have is that whenever I click the button that runs it, my program will copy that into the folder, that my program is in - and runs it there (like "D:\MyProgram\externprogram.exe"). Unfortuanally this extern program relies on other stuff being next to it and I don't want to copy that other stuff into my program folder too.

Is there any way to run a program whereever it is instead of copying it?

Upvotes: 1

Views: 82

Answers (2)

Berkay Yaylacı
Berkay Yaylacı

Reputation: 4513

Try to set WorkingDirectory;

using (Process pp = new Process())
       {
           pp.StartInfo.FileName = "externalAppUrl";
           pp.StartInfo.WorkingDirectory = "directoryNameToRun";
           pp.Start();
       } 

For More information check here,

Hope helps.

Upvotes: 1

Christoph K
Christoph K

Reputation: 354

Is there any way to run a program whereever it is instead of copying it?

Yes

The Process class has a constructor parameter called ProcessStartInfo. The ProcessStartInfo has a Property called WorkingDirectory. This property sets a value, that the Process should start in this directory. It won't copy any files to your directory.

Usage

ProcessStartInfo startInfo = new ProcessStartInfo(PathToYourExe) 
{
  WorkingDirectory = "The directory from the exe." 
}

Starting your process is now easy.

using(Process process = Process.Start(startInfo))
{
  // Your code while process will run.
}

Upvotes: 0

Related Questions