简体   繁体   中英

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.

See the WorkingDirectory property of ProcessStartInfo . Eg

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

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

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

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
ProcessStartInfo psi = new ProcessStartInfo(@"D:\application\app.exe") { WorkingDirectory = @"C:\" };
Process.Start(psi);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM