The Path class contains an interesting collection of methods, which you can think of as utilities. The Path class’s methods perform simple tasks such as retrieving a file’s name and extension, returning the full path description of a relative path, and so on. The Path class’s members are shared, and you must specify the path on which they will act as an argument.
The Path class exposes the following properties. Notice that none of these properties applies to a specific path; they’re general properties that return settings of the operating system. The FileSystem component doesn’t provide equivalent properties to the ones discussed in this section.
This property returns the directory separator character, which is the backslash character (\).
This property returns the list of invalid characters in a path as an array of the following characters:
/ \ ” < > —
You can use these characters to validate user input or pathnames read from a file. If you have a choice, let the user select the files through the Open dialog box, so that their pathnames will always be valid.
These properties return the separator characters that appear between multiple paths (:) and volumes (;), respectively.
The most useful methods exposed by the Path class are utilities for manipulating filenames and pathnames, described in the following sections. Notice that the methods of the Path class are shared: You must specify the path on which they will act as an argument.
This method changes the extension of a file. Its syntax is as follows:
newExtension = Path.ChangeExtension(path, extension)
The return value is the new extension of the file (a string value), and you can examine it from within your code to make sure that the operation completed successfully. The first argument is the file’s path, and the second argument is the file’s new extension. If you want to remove the file’s extension, set the second argument to Nothing. The following statement changes the extension of the specified file from .bin to .dat:
Dim path As String = "c:\My Documents\NewSales.bin"
Dim newExt As String = ".dat"
This method combines two path specifications into one. Its syntax is as follows:
newPath = Path.Combine(path1, path2)
Use this method to combine a folder path with a file path. The following expression will return the highlighted string:
Notice that the Combine method inserted the separator, as needed. It’s a simple operation, but if you had to code it yourself, you’d have to examine each path and determine whether a separator must be inserted.
This method returns the directory name of a path. The following statement:
will return this string:
These two methods return the filename in a path, with and without its extension, respectively.
This method returns the full path of the specified path; you can use it to convert relative pathnames to fully qualified pathnames. The following statement returned the highlighted string on my computer (it will be quite different on your computer, depending on the current directory):
C:\WorkFiles\Learn VB\Chapters\Chapter 11\Projects\Test.txt
The pathname passed to the method as an argument need not exist. The GetFullPath method will return the fully qualified pathname of a nonexistent file, as long as the path doesn’t contain invalid characters.
The GetTempFile method returns a unique filename, which you can use as a temporary storage area from within your application. The name of the temporary file can be anything, because no user will ever access it. In addition, the GetTempFile method creates a zero-length file on the disk, which you can open with the Open method. A typical temporary filename is the following:
It was returned by the following statement on my system:
The GetTempPath method returns the system’s temporary folder. All temporary files should be created in this folder, so that the operating system can remove them when it’s running out of space. Your applications should remove all the temporary files they create, but more often than not, programmers leave temporary files around.
This method returns a True/False value, indicating whether a path includes a file extension.