-1

can someone explain what for is startInfo.WorkingDirectory in ProcessStartInfo, not sure how to use it.

var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "folder\\code.exe");

I want run one .exe file

ProcessStartInfo startInfo = new ProcessStartInfo(Environment.ExpandEnvironmentVariables(filePath));
startInfo.WorkingDirectory = Environment.ExpandEnvironmentVariables(@"%AppData%\\folder\\");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);

what the difference

ProcessStartInfo startInfo = new ProcessStartInfo(filePath);
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
nikorio
  • 671
  • 4
  • 16
  • 28

2 Answers2

2

Basically it sets the working directory for the exe. Think a windows shortcut, it has a working directory there as well. The working directory mostly contains what path is "home", and more imprtantly, it controls where the app first looks for DLL files. If I recall my path rules correctly, it searches the app's working directory, system,system32, and any directory in the PATH environment variable.

Hope this helps!

Trey
  • 413
  • 4
  • 12
  • Hello, so I'm not sure how do write directory for DLL files, which one is correct if I want locate it in C:\Users\User\AppData\Roaming\myFolder `Environment.ExpandEnvironmentVariables("Roaming\\myFolder\\");` and not `Environment.ExpandEnvironmentVariables(@"%AppData%\\Roaming\\myFolder");` right? – nikorio Dec 12 '16 at 17:04
  • OK, we are scope creeping a bit here.. but here is a good read: – Trey Dec 12 '16 at 19:41
  • https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx – Trey Dec 12 '16 at 19:42
1

The working directory is where it starts, in your second example it starts it where your current directory is, in the first one, you get a folder applicationdata, (so expandenvironmentalvariables probably isnt necessary, as thats more for %windows% etc,) but, you start the exe in the folder of your code.exe, in the second its where ever the app your code is using as its working folder..

Lets say your current app is app.exe, it lives in c:\myapp

you start your app and its currently working in c:\myapp as either you ran it from explorer, or thats the short cut.

in example one, your process would go to your user\appliationdata\folder\ and run code.exe

in example two, it would run code.exe from inside c:\myapp

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • Hi, so if my application executable with installation automatically goes to the the ProgramData, but wants use several DLL files loaded from own resources. How to get this directory by environment path if I want load and read from C:\Users\User\AppData\Roaming\myFolder I'm not sure how to load and allow to read DLL files for my application in ProgramData from C:\Users\User\AppData\Roaming\myFolder or better to write DLL files to the same `SpecialFolder.CommonApplicationData` but if I have no permission to write something to Program Data how to do it for executable for user computer – nikorio Dec 12 '16 at 17:49