olidev
olidev

Reputation: 20644

locate to a folder and execute an application C#

In my application, the user can select a program:

D:/application/app.exe

I would like to execute it such that the same situation that I have to do on CMD, it will show:

C:/

then I have to do: D:

then:

D:/application/app.exe

The application can be only run on its folder for connecting with other libraries.

How can I make it possible to execute it from C# in such a way that it locate to the D:/application first and then execute: app.exe?

Thanks in advance.

Upvotes: 1

Views: 107

Answers (4)

Paul Ruane
Paul Ruane

Reputation: 38590

See the WorkingDirectory property of ProcessStartInfo. E.g.

Process.Start(new ProcessStartInfo {
                                       WorkingDirectory = @"D:\application",
                                       FileName = "app.exe"
                                   }

Upvotes: 3

Adi Lester
Adi Lester

Reputation: 25201

The Path class can help you parse and manipulate your input path.

Path.GetPathRoot("D:\MyApp\App.exe") --> D:\
Path.GetDirectoryName("D:\MyApp\App.exe") --> D:\MyApp

Upvotes: 2

ken2k
ken2k

Reputation: 48985

ProcessStartInfo psi = new ProcessStartInfo(@"D:\application\app.exe") { WorkingDirectory = @"C:\" };
Process.Start(psi);

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160892

You can set the working directory when you start a new process:

Process.Start(new ProcessStartInfo()
{
        FileName = @"D:\application\app.exe",
        WorkingDirectory = @"D:\application",
        //...
});

Upvotes: 3

Related Questions