Recipe12.15.Parsing a Path


Recipe 12.15. Parsing a Path

Problem

You need to separate the constituent parts of a path and place them into separate variables.

Solution

Use the static methods of the Path class:

 public static void ParsePath(string path) {     string root = Path.GetPathRoot(path);     string dirName = Path.GetDirectoryName(path);     string fullFileName = Path.GetFileName(path);     string fileExt = Path.GetExtension(path);     string fileNameWithoutExt = Path.GetFileNameWithoutExtension(path);     StringBuilder format = new StringBuilder( );     format.Append("ParsePath of {0} breaks up into the following pieces:" +                   Environment.NewLine + "\tRoot: {1}" +                   Environment.NewLine + "\t");     format.Append("Directory Name: {2}" +                   Environment.NewLine + "\tFull File Name: {3}" +                   Environment.NewLine + "\t");     format.Append("File Extension: {4}" +                   Environment.NewLine + "\tFile Name Without Extension: {5}" +                   Environment.NewLine + "");     Console.WriteLine(format.ToString( ),path,root,dirName,            fullFileName,fileExt,fileNameWithoutExt); } 

If the string @C:\test\tempfile.txt is passed to this method, the output looks like this:

 ParsePath of C:\test\tempfile.txt breaks up into the following pieces:         Root: C:\         Directory Name: C:\test         Full File Name: tempfile.txt         File Extension: .txt         File Name Without Extension: tempfile 

Discussion

The Path class contains methods that can be used to parse a given path. Using these classes is much easier and less error-prone than writing path-and filename-parsing code. There are five main methods used to parse a path: GetPathRoot, GetDirectoryName, GetFileName, GetExtension, and GetFileNameWithoutExtension. Each has a single parameter, path, which represents the path to be parsed:


GetPathRoot

This method returns the root directory of the path. If no root is provided in the path, such as when a relative path is used, this method returns an empty string, not null.


GetDirectoryName

This method returns the complete path for the directory that the file is in.


GetFileName

This method returns the filename, including the file extension. If no filename is provided in the path, this method returns an empty string, not null.


GetExtension

This method returns the file's extension. If no extension is provided for the file or no file exists in the path, this method returns an empty string, not null.


GetFileNameWithoutExtension

This method returns the root filename without the file extension.

Be aware that these methods do not actually determine whether the drives, directories, or even files exist on the system that runs these methods. These methods are string parsers and if you pass one of them a string in some strange format (such as \\ZY:\foo), it will try to do what it can with it anyway:

 ParsePath of \\ZY:\foo breaks up into the following pieces:         Root: \\ZY:\foo         Directory Name:         Full File Name: foo         File Extension:         File Name Without Extension: foo 

These methods will, however, throw an exception if illegal characters are found in the path.

To determine whether files or directories exist, use the static Directory.Exists or File.Exists method.

See Also

See the "Path Class" topic 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