More than one important way for a .NET process

In the project that I am currently working on, I begin an external process. However, the external process is an EXE of a complex program that downloads current user information from a user folder. The desktop shortcut for the program resolves the issue by setting the "Target:" parameter to X:\exepath\prgm.exe and setting the "Enter input" parameter to the user path, X:\exepath\users\username .

I am currently running a process like this:

 Process p = new Process(); p.StartInfo = new ProcessStartInfo( "X:\exepath\prgm.exe" ); p.StartInfo.WorkingDirectory = "X:\exepath\users\username"; p.Start(); while (!p.HasExited) { } 

However, when the process starts, the program starts the search for all resources in WorkingDirectory instead of pulling user-generated content from this folder and all other content from the directory where the EXE is located. This assumes that the Working Directory and the system shortcut "Start In:" parameter behave differently.

Is there a way to reproduce this behavior using a C # process? Also, is it possible to create a shortcut in C # that I could start with my process call?

Please let me know if further information is helpful.

EDIT -

After some trial and error, I decided to use WSH to create a shortcut and run it. WSH uses the name WorkDirectory for the value of the Start at: parameter. It behaves the same under the hood, like the process execution in my code above. I am still getting the error.

+6
source share
2 answers

I solved a problem that was not related to the creation of the process in the end. Actually, the root cause is a bit embarrassing, but potentially educational, so I will give an explanation.

The code I posted on the OP was code example to illustrate the problem. In my actual project, I retrieved the ExePath and UserPath from the registry. The project is a Chooser tool for switching between several installed versions of third-party software and reads / edits these registry keys to do its job.

When I wrote code that writes to the registry, I used DirectoryInfo.FullPath , which returned "X:\ExePath" instead of "X:\ExePath\" . This caused the program to not find the necessary files from the ExePath folder, looking for X: \ ExePathsettings.inf instead of "X: \ ExePath \ settings.inf". I inserted a backslash in my code and in the existing registry and everything works fine.

Lesson learned: Always check your path values โ€‹โ€‹very carefully.

+1
source

The difference is probably related to the use of the shell process: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute.aspx

The WorkDirectory property behaves differently if UseShellExecute is true than when UseShellExecute is false. When UseShellExecute is true, the WorkDirectory property determines the location of the executable. If WorkDirectory is an empty string, the current directory is understood as containing the executable.

When UseShellExecute is false, the WorkingDirectory property is not used to search for an executable file. Instead, it is used by a process that is running and only makes sense within the context of a new process.

I suspect that you set p.StartInfo.UseShellExecute to false, it may behave the way you want.

+1
source

Source: https://habr.com/ru/post/891314/


All Articles