23850

How to resolve environment variables programatically in C# without using Environment Enum? [duplicat

<strong>Possible Duplicate:</strong> 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 <strong>NOT</strong> 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#.

Answer1:

<s>You can use System.IO.Path.GetFullPath. For example:

var resolvedPath = Path.GetFullPath("%WINDIR%");

</s> Or if you just want to expand variables in a string, use Environment.ExpandEnvironmentVariables:

var expanded = ExpandEnvironmentVariables( "This is my %WINDIR% in %SYSTEMROOT%, and temp is %TEMP%")

Answer2:

Environment.ExpandEnvironmentVariables("%windir%\SomeFolder")

Recommend

  • select max salary in sql
  • HyperLedger-Fabric ChainCode Deployment - Base64 error
  • Setting resource paths programatically in spring MVC
  • How to determine if a ViewController is top level or not?
  • Opencart: How to include a script on certain pages?
  • UIActionSheet orientation
  • Logging to remote asp.net Web Page
  • iPhone set interfaceorientation programmatically
  • Resize of Iframe dynamically depending on the content of the Iframe of the Iframe's src URL
  • How to call JSON webservice from background in iPhone
  • Using a WebService with Akka Actors and the play framework
  • Android - What should I use to get data from remote db?
  • Read Values from .csv file and convert them to float arrays
  • android with msaccess
  • How to save a large fractal image with the least possible memory footprint?
  • Embed multiple jpeg images into EXCEL programmatically?
  • How to use java.time.LocalDate on a Play Framework JSON Rest?
  • iOS Date formatting
  • c# bind winforms chart to list of objects
  • Dump Chrome dev console to text file?
  • Java Garbage collection, setting reference to null
  • How to determine the CCSID used in CPYFRMIMPF command?
  • How to discover Font Type?
  • ConnectivityManager.CONNECTIVITY_ACTION deprecated
  • android Navigation Bar hiding and persantage of usable screen overlap
  • Cannot access Public method in Web Control's Page_Load event
  • How to use jQuery's $.post() method with async/await and typescript
  • Overlapping controls in Windows XP
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Custom Tabgroup Appcelerator
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • How to rebase a series of branches?
  • Is there a amazon webstore API for customers?
  • Join two tables and save into third-sql
  • Convert array of 8 bytes to signed long in C++
  • Understanding cpu registers
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices