Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing directory

Tags:

c#

directory

I have a C# program that is running and I want to launch another executable in different directory.

I have this code on event:

 string path = "Y:\Program\test.exe";
 Process.Start(path);

Problem is that in order for program to work right it need to take information from settings.ini where the exe file is located but it takes settings.ini from program folder with which I am trying to launch second program. test.exe is working fine when I am opening it from its folder by double click. What could be the problem?

like image 433
WhizBoy Avatar asked Jul 12 '26 20:07

WhizBoy


1 Answers

You need to tell the process what the working directory is via ProcessStartInfo.WorkingDirectory:

var processStartInfo = new ProcessStartInfo
{
    WorkingDirectory = @"Y:\Program",
    FileName = @"Y:\Program\test.exe",
};

Process.Start(processStartInfo);

Edit:

In order to get the directory from the user, you can use DirectoryInfo.FullName:

var userFileInfo = new FileInfo(userInsertedVariableHere);
var parentDirectory = userFileInfo.Directory.FullName;
like image 197
Yuval Itzchakov Avatar answered Jul 14 '26 09:07

Yuval Itzchakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!