9.12 Manipulate Strings Representing file names


9.12 Manipulate Strings Representing file names

Problem

You want to retrieve a portion of a path or verify that a file path is in a normal (standardized) form.

Solution

Process the path using the System.IO.Path class. You can use Path.GetFileName to retrieve a file name from a path, Path.ChangeExtension to modify the extension portion of a path string, and Path.Combine to create a fully qualified path without worrying about whether or not your directory includes a trailing directory separation (\) character.

Discussion

File paths are often difficult to work with in code because there is an essentially unlimited number of ways to represent the same directory. For example, you might use an absolute path (c:\temp), a UNC path (\\MyServer\\MyShare\temp), or one of many possible relative paths (c:\temp\MyFiles\..\ or c:\temp\MyFiles\..\..\temp).

The easiest way to handle file system paths is to use the static methods of the Path class to make sure you have the information you expect. For example, here's how you take a file name that might include a qualified path and extract just the file name:

 string  file name = @"..\System\MyFile.txt"; file name = Path.GetFileName(file name); // Now file name = "MyFile.txt" 

And here's how you might append the file name to a directory path using the Path.Combine method:

 string filename = @"..\..\myfile.txt"; string fullPath = @"c:\Temp"; file name = Path.GetFileName(file name); fullPath = Path.Combine(fullPath, filename); // (fullPath is now "c:\Temp\myfile.txt") 

The advantage of this approach is that a trailing backslash (\) is automatically added to the path name if required. The Path class also provides the following useful methods for manipulating path information:

  • ChangeExtension modifies the current extension of the file in a string. If no extension is specified, the current extension will be removed.

  • GetDirectoryName returns all the directory information, which is the text between the first and last directory separators (\).

  • GetFileNameWithoutExtension is similar to GetFileName , but it omits the extension.

  • GetFullPath has no effect on an absolute path, and it changes a relative path into an absolute path using the current directory. For example, if c:\Temp\ is the current directory, calling GetFullPath on a file name such as test.txt returns c:\Temp\ test.txt.

  • GetPathRoot retrieves a string with the root (for example, "C:\"), provided that information is in the string. For a relative path, it returns a null reference.

  • HasExtension returns true if the path ends with an extension.

  • IsPathRooted returns true if the path is an absolute path and false if it's a relative path.

    Note  

    In most cases, an exception will be thrown if you try to supply an invalid path to one of these methods (for example, paths that include illegal characters ). However, pathnames that are invalid because they contain a wildcard character (* or ?) will not cause the methods to throw an exception.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

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