Properly Set Process WorkingDirectory
Asked Answered
D

3

6

When setting a process, it seems like I'm not using that variable, WorkingDirectory, in the proper manner. I get the error (with a catch)

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

However, in the folder Stress, there is a Test.exe... , so I really don't understand the meaning of this.

Why is there a failure and how can it be resolved?


The code is the following (note that I replaced variable with the direct string contents 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 the WorkingDirectory is affected by UseShellExecute, however I respected this.

Decagram answered 27/5, 2015 at 13:11 Comment(8)
Show us the code you're using to create the process.Chadwell
see the original post!!Decagram
try instead of using workingdirectory to just use the entire path for the filenameCismontane
I would use the entire path for the filename, and also include the working directory. And for both of those I would use Path.Combine to build up the paths.Heterocercal
Don't do Directory.GetCurrentDirectory() + "\\" + "Stress", it's why Path.combine exists.Oversubscribe
@NissaArmelon Are you sure the generated path exists?Chadwell
ok I now use pathcombine, and I'm testing the restDecagram
it all says they exist, but I always get the error saying it cannot find the filesDecagram
H
0

Try adding Directory.Exists( proc.StartInfo.WorkingDirectory ) after you set it. Does Test.exe exist in that directory?

Also try:

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

check File.Exists( filename );

perhaps 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 to see if the file and folder exist.

Heterocercal answered 27/5, 2015 at 13:27 Comment(2)
ok here is the result after all the tries. They all exist when I do as you say. However it still doesn't work (even if File.Exists return true), when I execute the code it says CurrentDirectory='', Native error= Cannot find the specified file why does it says CurrentDirectory="" when I set the WorkingDirectory? aren't they the same?Decagram
I do this string b = Path.Combine (proc.StartInfo.WorkingDirectory, proc.StartInfo.FileName); if (File.Exists( b )) { b = ""; } proc.Start (); it says path exist because it goes in the if. then when I start the process it says this : Native error= The system cannot find the file specified.Decagram
D
0

Found my error :

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

process.FileName = "mono"
process.Argument = "nameOfExe param1 param2..."
Decagram answered 27/5, 2015 at 14:47 Comment(0)
C
0

Since UseShellExecute is false:

The WorkingDirectory property behaves differently depending on the value of the UseShellExecute property. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, it's assumed that the current directory contains the executable.

When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used only by the process that is started and has meaning only within the context of the new process. When UseShellExecute is false, the FileName property can be either a fully qualified path to the executable, or a simple executable name that the system will attempt to find within folders specified by the PATH environment variable. The interpretation of the search path depends on the operating system. For more information, enter HELP PATH or man sh at a command prompt.

https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-diagnostics-processstartinfo-useshellexecute#workingdirectory

Curtsy answered 29/8 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.