Possible Duplicate:
Expand environment variable for My Documents
We often use %% delimited environment variables in Command prompt in Windows Xp, 7, Vista, Server 2003-2008R2 and possibly in future windows versions. Examples are:
%windir%
%systemroot%
%temp%
I need to be able to programatically resolve these environment variables using C#.
Please do not suggest Environmen.SpecialFolder
, because i am getting values from a webservice in the above formats and i need to able to programmatically resolve them and determine physical location of these paths on server. My scenario is a class libarary project and i may NOT require/be able to use Server.MapPath
I am looking for a generic method builtin or custom that can help me resolve all environment variables pro grammatically and calculate local physical paths from these variables just Command Prompt or Windows Explorer or Run Command does !
I have seen questions similar to these on StackOverFlow but couldn't find any marked answers. Please note that Environment.Xxxxx Enum
doesnt cater for the scenario i am working through.
Any help is appreciated. I am using .NET 4.0 Complete Framework Profile with C#.
DESCRIPTION. The setenv() function shall update or add a variable in the environment of the calling process. The envname argument points to a string containing the name of an environment variable to be added or altered. The environment variable shall be set to the value to which envval points.
Environment variable is a global variable that can affect the way the running process will behave on the system.
The dsadmin command can be used for deleting an environment variable in a particular project.
You can use System.IO.Path.GetFullPath
. For example:
var resolvedPath = Path.GetFullPath("%WINDIR%");
Or if you just want to expand variables in a string, use [`Environment.ExpandEnvironmentVariables`](http://msdn.microsoft.com/en-us/library/system.environment.expandenvironmentvariables.aspx):
var expanded = Environment.ExpandEnvironmentVariables(
"This is my %WINDIR% in %SYSTEMROOT%, and temp is %TEMP%")
Environment.ExpandEnvironmentVariables("%windir%\SomeFolder")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With