locked
Using environment PATH variables with System.Diagnostics.Process.Start RRS feed

  • Question

  • Hello,

    I try to run an application in my code.

    Looks like:

    System.Diagnostics.Process.Start(@"C:\Windows\SkeletonApp.exe");

    Unfortunately I don't get it working with class variables like:

    System.Diagnostics.Process.Start(@"%WINDIR%\SkeletonApp.exe"

    It looks like all kinds of Path variables are not working... I tried it with: %Windir%, %appdata%, %programfiles

    Friday, June 21, 2013 11:08 PM

Answers

All replies

  • You can explicitly read the environment variable and then contruct the path to your exe.

    To read the env variable use

    http://msdn.microsoft.com/en-us/library/77zkk0b6.aspx

    then concatenate this with "SkeletonApp.exe" name to construct the full path dynamically.


    val it: unit=()

    Saturday, June 22, 2013 12:54 AM
  • What you are looking for is the System.Environment.SpecialFolder enumeration.

    var pathToExe = 
        System.IO.Path.Combine(System.Environment.SpecialFolder.Windows, "SkeletonApp.exe");
    
    System.Diagnostics.Process.Start(pathToExe);



    Saturday, June 22, 2013 2:55 AM
  • Thank you for your hints. It's working very well.

    Additional question: In batch files I can use "%~dp0 " to use the same directory where the batch is located.

    Can I do something similar in C#?
    I built an installer for my application and I try to run my SkeletonApp from the same directory like my normal application. f.e. C:\program Files\Kinect-tool\

    But depending on the used OS the installation files could also be located in C:\Program Files (x86)\

    I actually placed the SkeletonApp in %appdata%\Kinect-tool cause it's the same in x64 and x86 Versions.

    Saturday, June 22, 2013 9:57 AM
  • For that, you can use the AppDomain.CurrentDomain.BaseDirectory property :

    var pathToExe = AppDomain.CurrentDomain.BaseDirectory;
    Saturday, June 22, 2013 12:54 PM