Recipe12.16.Parsing Paths in Environment Variables


Recipe 12.16. Parsing Paths in Environment Variables

Problem

You need to parse multiple paths contained in environment variables, such as PATH or Include.

Solution

You can use the Path.PathSeparator field or the ; character to extract individual paths from an environment variable with a value that consists of multiple paths and place them in an array. Then you can use a foreach loop to iterate over each individual path in the PATH environment variable and parse each path. This process is illustrated by the ParsePathEnvironmentVariable method:

 public static void ParsePathEnvironmentVariable( ) {     string originalPathEnv = Environment.GetEnvironmentVariable("PATH");     string[] paths = originalPathEnv.Split(new char[1] {Path.PathSeparator});     foreach (string s in paths)     {         string pathEnv = Environment.ExpandEnvironmentVariables(s);         Console.WriteLine("Path = " + pathEnv);         if(pathEnv.Length > 0)         {             Console.WriteLine("Individual Path = " + pathEnv);         }         else         {             Console.WriteLine("Skipping blank environment path details " +                     " as it causes exceptions…");         }     } } 

If the PATH environment variable contains the following:

 PATH=Path=C:\WINDOWS\system32;C:\WINDOWS 

then the output of the ParsePathEnvironmentVariable method is as follows:

 Individual Path = C:\WINDOWS\system32 Individual Path = C:\WINDOWS 

Discussion

When working with environment variables in particular, there are a number of cases in which several paths may be concatenated and you need to parse each one individually. To distinguish each individual path from the others, Microsoft Windows uses the semicolon character. (Other operating systems might use a different character; Unix, Linux, and Mac OS X use a colon.) To make sure that we always use the correct path-separation character, the Path class contains a public static field called PathSeparator. This field contains the character used to separate paths in the current platform. This field is marked as read-only, so it cannot be modified.

To obtain each individual path contained in a single string, use the Split instance method from the String class. This method accepts a param array of character values that are used to break apart the string instance. These individual strings containing the paths are returned in a string array. Then we simply use the foreach loop construct to iterate over each string in this string array and use the static method ExpandEnvironmentVariables of the Environment class to operate on each individual path string. This static method ensures that any environment variables such as %SystemDrive% are converted to their equivalent value, in this case C:.

See Also

See the "Path Class" and "Environment Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net