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.
Properties
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.
DirectorySeparatorChar
This property returns the directory separator character, which is the backslash character (\).
InvalidPathChars
This property returns the list of invalid characters in a path as an array of the following characters:
/ \ " < > —
Code language: HTML, XML (xml)
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.
PathSeparator, VolumeSeparatorChar
These properties return the separator characters that appear between multiple paths (:) and volumes (;), respectively.
Methods
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.
ChangeExtension
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"
Path.ChangeExtension(path, newExt)
Code language: JavaScript (javascript)
Combine
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:
Path.Combine("c:\textFiles", "test.txt")
c:\textFiles\test.txt
Code language: CSS (css)
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.
GetDirectoryName
This method returns the directory name of a path. The following statement:
Path.GetDirectoryName("C:\folder1\folder2\folder3\Test.txt")
Code language: CSS (css)
will return this string:
C:\folder1\folder2\folder3
GetFileName, GetFileNameWithoutExtension
These two methods return the filename in a path, with and without its extension, respectively.
GetFullPath
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):
Console.WriteLine(Path.GetFullPath("..\..\Test.txt"))
C:\WorkFiles\Learn VB\Chapters\Chapter 11\Projects\Test.txt
Code language: CSS (css)
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.
GetTempFile, GetTempPath
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:
C:\DOCUME˜1\TOOLKI˜1\LOCALS˜1\Temp\tmp105.tmp
Code language: CSS (css)
It was returned by the following statement on my system:
Debug.WriteLine(Path.GetTempFile)
Code language: CSS (css)
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.
HasExtension
This method returns a True/False value, indicating whether a path includes a file extension.