5

I'm wondering is there any way to use the normal shortcuts form windows like %PROGRAMFILES%, %APPDATA%,.... when using System.Diagnostics.Process.Start ?

What I want to do there is using one of those shortcuts to dynamically create the path that is being used to tart the program I want to start with Process.Start. Example:

System.Diagnostics.Process.Start("%PROGRAMFILES%\MyApp\MyApp.exe");

Edit: As a comment to the accepted answer:

As one important thing was mentioned in the comments I want to also put it here: If the solution does not work as the file is not found, one should print out the result of the System.Environment.ExpandEnvironmentVariables command. It can be that it points unintendedly to the x86 program files location instead of the program files location (or vice versa) depending on if for the application itself (project properties) "Prefer 32-bit" or platform target is set accordingly. If that is kept in mind the solution works quite nicely.

Thomas
  • 2,886
  • 3
  • 34
  • 78

3 Answers3

6

Use System.Environment.ExpandEnvironmentVariables to perform the expansion first, then pass the result to Process.Start:

System.Diagnostics.Process.Start(
    System.Environment.ExpandEnvironmentVariables(@"%PROGRAMFILES%\MyApp\MyApp.exe"));
lc.
  • 113,939
  • 20
  • 158
  • 187
  • tried it but didn't work (tried the same string I used in windows explorer and worked) – Thomas Jul 16 '14 at 06:44
  • Not sure what to say - and I don't know what "didn't work" means - ExpandEnvironmentVariables works for me. You sure it's pointing to the *right* program files folder (i.e. are you running this 32-bit where you need the 64-bit folder)? – lc. Jul 16 '14 at 06:49
  • interesting. Yepp just made sure that its in the right folder. Used: System.Diagnostics.Process.Start(System.Environment.ExpandEnvironmentVariables(@"%PROGRAMFILES%\CompanyName\MyApp\AppClient\MyApp.GUI.exe")); then it says "System couldn't find the file" (translated the error to english so english version could vary a bit there). The file itself is in the Progams\CompanyName\MyApp\AppClient folder. – Thomas Jul 16 '14 at 06:54
  • What's the result of the call to `Environment.ExpandEnvironmentVariables`? – lc. Jul 16 '14 at 06:55
  • tnx that was the spöitopm there (didnt think of printing that out). Interestingly it points to the 32 bit folder of programs oO (if I use the same command in explorer it points to the 64 bit). is there a way to change that behaviour? – Thomas Jul 16 '14 at 06:57
  • Ah found it. In the aplication I had a check in "Prefer 32-bit" for platform target. Removing that removed the problem. Tnx there. – Thomas Jul 16 '14 at 07:00
  • 1
    @ThomasE. Well, one way would be to make your application run 64-bit :) – Luaan Jul 16 '14 at 07:00
  • Possibly related http://stackoverflow.com/questions/2003573/how-to-start-a-64-bit-process-from-a-32-bit-process – lc. Jul 16 '14 at 07:01
  • yepp thought the application was 64 bit already. but interesting bit there though that it is depending on the application itself how that one is interpreted. – Thomas Jul 16 '14 at 07:01
  • @ThomasE. .NET applications are capable of running both 32-bit and 64-bit by default, based on the environment (not to mention completely different OSes and CPUs). It's rather convenient in some ways :) – Luaan Jul 16 '14 at 07:03
  • Yepp although one needs to watch out there a bit more in cases like this because of that^^ I'm going to put the note about making sure that the environemnt.ExpandEnvironmentVariables is pointing to the right location into the question (as a note there). I know from experience that comments are often overlooked. – Thomas Jul 16 '14 at 07:29
0

I am almost sure this does not work, however, there is an environment class in the .NET library that can return the information you are looking for.

Probably like:

// Change the directory to %WINDIR%                         
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");        

If you use 'programfiles' instead it might work (could not check here).

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
0

You can use

Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)`

To get the path to a special folder (in this case, 32-bit program files directory). There's more in that class that would be of help as well.

Also, I expect that Process.Start with the non-expanded path will work fine if you use UseShellExecute - the shell is capable of expanding paths on its own.

However, this is still probably a bad solution. What if the user installed your target application somewhere else? Are you sure there's no better way to get path to the application?

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • UseShell resulted in the same error I mentioned above in the expandenvironmentvariables answer. Can it be that it is not working because I'm not in an english environment (thus nonenglish windows)? – Thomas Jul 16 '14 at 06:55