Sunday, February 22, 2009

How-To Get Environment Variables And Resolve System Paths

Tonight a useful trick in order to get Environment Variables and Resolve System Paths like :

  • %windir%
  • %Programfiles%
  • ..
MSDN :

Environment Class

Provides information about, and means to manipulate, the current environment and platform. This class cannot be inherited.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Display All Current Environment Variables

IDictionary dic = Environment.GetEnvironmentVariables();
foreach (String variable in dic.Values)
{
Console.WriteLine(variable);
}
String DirPath = "%ProgramFiles%";
String Expanded = Environment.ExpandEnvironmentVariables(DirPath);
Console.WriteLine(Expanded);

DirPath = "%windir%";
Expanded = Environment.ExpandEnvironmentVariables(DirPath);
Console.WriteLine(Expanded);

Source : http://blog.sb2.fr/post/2008/12/23/How-To-Get-Environment-Variables-And-Resolve-System-Paths.aspx

No comments:

Post a Comment