Recipe 12.7. Parsing File and Directory Paths


Problem

You need to extract a directory name from a full path to a file or get just the filename portion, and you don't want to mess with all of those backslashes.

Solution

Use the path-parsing methods found in the My.Computer. FileSystem object: CombinePath(), GetName(), and GetParentPath().

Discussion

As a programmer, you spend a lot of time manipulating string data. The .NET Framework has taken on itself some of the burden involved in specific types of string management. XML is a good example: you can use the XML objects included in .NET instead of stringing together the various components yourself. .NET provides similar convenience features for path-string manipulation.

The My namespace includes three methods designed to help you manage path strings. None of them compares the supplied paths to existing files or directories on the local or network file system; they are purely string-manipulation methods. Here are the methods:


My.Computer.FileSystem.CombinePath( )

The CombinePath( ) method accepts an absolute path and a relative path to attach to the end of the absolute path. It returns the combined path with the relative part attached to the end of the absolute part, with any necessary "\" characters added where needed. The relative part may be a directory name or a filename. For example:

 Dim newPath As String = _    My.Computer.FileSystem.CombinePath( _    "C:\temp", "WorkFiles\TodaysWork.txt") MsgBox(newPath)    ' Displays: "C:\temp\WorkFiles\TodaysWork.txt" 

If you provide a relative path for the first "absolute" argument, CombinePath( ) first modifies the argument so that it indicates a directory within the current directory as understood by the application. For instance, if the current directory is C:\temp, the statement:

 MsgBox(My.Computer.FileSystem.CombinePath( _    "part1", "part2") 

displays C:\temp\part1\part2.


My.Computer.FileSystem.GetName( )

This method extracts the final component of a supplied path and returns it:

 ' ----- Displays: part2 MsgBox(My.Computer.FileSystem.GetName( _    "C:\temp\part1\part2")) 

You can supply absolute or relative paths to the GetName( ) function. The result of the function is always the final path component of whatever string you send.


My.Computer.FileSystem.GetParentPath( )

The GetParentPath( ) method returns everything except the final component of a supplied path. That is, it returns the directory that contains the final path component. If there is a trailing backslash, it is removed:

 ' ----- Displays: C:\temp\part1 MsgBox(My.Computer.FileSystem.GetParentPath( _    "C:\temp\part1\part2")) 

You can supply absolute or relative paths to the GetParentPath( ) function. The result of the function is always the parent-path component of whatever string you send. If you provide a string that contains only a single relative component (such as "MyDirectory" or ".."), this function returns a zero-length string.

Although these three methods deal exclusively with strings and not with actual paths, they do perform some minimal text analysis to ensure you process valid paths. If you attempt to use Unix-style forward slashes ("/") in your paths instead of the Windows-style backslash ("\"), these methods convert all "/" characters to "\" before generating results. Also, these methods raise an exception if you supply a URI-based file path (as in file://system/directory/file).




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

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