Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting application with shortcuts (%PROGRAMFILES%) being used

Tags:

c#

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.

like image 277
Thomas Avatar asked Jul 16 '14 06:07

Thomas


1 Answers

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"));
like image 124
lc. Avatar answered Oct 29 '22 17:10

lc.