Search code examples
c#.netvisual-studio-2008application-settings

Can I use environment variables in a .settings file?


.NET allows me to create and access values that are persistent between sessions.
Is it possible to use environment variables such as %WINDIR%, %APPDATA% and others?
If so, how would I go about doing this?


Solution

  • If you have this in the config:

     <appSettings>
       <add key="SomePath" value="%TEMP%"/>
     </appSettings>
    

    Then from code you'd do something like:

    string path = ConfigurationManager.AppSettings["SomePath"];
    string fullPath = System.Environment.ExpandEnvironmentVariables(path);