Friday, 27 February 2015

How to create relative path from absolute path in Web Application using environment variables


Windows Environment variables:
In C#, you could use windows environment variables for configuration.
   
In below example, "Testdata" is the folder which should be configured in windows “Program data” folder.
And exact location of Program data will be determined using C# Environment.ExpandEnvironmentVariables() method.


Setting in Web.Config or App.Config:

<add key="TestFolderPath" value="%Program Data%\Testdata" /> 

//Here "%ProgramData%\Testdata" is absolute path

Reading relative path:
var TestFolderPath= Environment.ExpandEnvironmentVariables(
ConfigurationManager.AppSettings[“TestFolderPath”])

//Output of TestFolderPath=> "C:\ProgramData\Testdata"
//For each machine this path may change based on “Program data” location.



No comments:

Post a Comment