How to properly install WorkDirectory process in C #

When setting up the process, it seems like I'm not using this WorkDirectory variable correctly. I get an error (with a trick)

ApplicationName = 'Test.exe', CommandLine = '/ d = 1', CurrentDirectory = 'C: \ Users \ mb \ Desktop \ integration \ Tests \ dailyTest \ dailyTest \ Bin \ Debug \ Stress', Native error = system cannot find the specified file.

However, in the Stress folder, I have Test.exe .. I really do not understand the meaning of this.

The code is as follows (note that I replaced the variable with the contents of a straight line for better understanding).

Process proc = new System.Diagnostics.Process(); proc.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory() + "\\" + "Stress"); proc.StartInfo.FileName = "Test.exe"; proc.StartInfo.Arguments = "/d=1"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = false; proc.Start (); proc.WaitForExit(); return proc.ExitCode; 

I know that UseShellExecute affects WorkDirectory, but I respected it.

+5
source share
2 answers

Try adding Directory.Exists( proc.StartInfo.WorkingDirectory ) after installing it. Does Test.exe in this directory?

Also try:

 string filename = Path.Combine( Directory.GetCurrentDirectory(), "Stress", "test.exe" ); check File.Exists( filename ); 

it is possible to use filename as proc.StartInfo.FileName

For your working directory, use: Path.Combine( Directory.GetCurrentDirectory(), "Stress" )

To clarify, I would say to use:

 proc.StartInfo.WorkingDirectory = Path.Combine( Directory.GetCurrentDirectory(), "Stress"); proc.StartInfo.FileName = Path.Combine( Directory.GetCurrentDirectory(), "Stress", "Test.exe" ); bool folderExists = Directory.Exists( proc.StartInfo.WorkingDirectory ); bool fileExists = File.Exists( proc.StartInfo.FileName ); 

You can debug information about whether a file and folder exist.

0
source

Found my error:

I was on Linux, so I need to indicate that my exe file is executed using "mono"

 process.FileName = "mono" process.Argument = "nameOfExe param1 param2..." 
0
source

All Articles