Path |
Name | Description |
---|---|
AltDirectorySeparator (get) | Returns the string for the alternative directory separator character. |
AltDirectorySeparatorChar (get) | Returns the character code for the alternative directory separator character. |
DirectorySeparator (get) | Returns the directory separator string for Windows. |
DirectorySeparatorChar (get) | Returns the directory separator character for Windows. |
PathSeparator (get) | The string used to seperate strings in an environment variable. |
PathSeparatorChar (get) | The character code used to separate strings in an environment variable. |
VolumeSeparator (get) | Returns the string version of the volume separator. |
VolumeSeparatorChar (get) | The character code used to separate volume labels from path information. |
Name | Description |
---|---|
ChangeExtension | Changes the extension of a path string. |
Combine | Combines two or more strings into a path. |
CombineArray | Combines an array of strings into a path. |
GetDirectoryName | Returns the directory information for the specified path string. |
GetExtension | Returns the extension of the specified path string. |
GetFileName | Returns the file name and extension of the specified path string. |
GetFileNameWithoutExtension | Returns the filename portion of a path without the filename extension. |
GetFullPath | Returns the absolute path for the specified path string. |
GetInvalidFileNameChars | Gets an array containing the characters that are not allowed in file names. |
GetInvalidPathChars | Gets an array containing the characters that are not allowed in path names. |
GetPathRoot | Returns the root directory information of the specified path. |
GetTempFileName | Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file. |
GetTempPath | Returns the path to the temporary folder for the current user. |
HasExtension | Determines whether a path includes a file name extension. |
IsPathRooted | Gets a value indicating whether the specified path string contains a root. |
A path can contain absolute or relative location information. Absolute paths fully specify a location: the file or directory can be uniquely identified regardless of the current location. Relative paths specify a partial location: the current location is used as the starting point when locating a file specified with a relative path. To determine the current directory, call Directory.GetCurrentDirectory.
Most members of the Path class do not interact with the file system and do not verify the existence of the file specified by a path string. Path class members that modify a path string, such as ChangeExtension, have no effect on names of files in the file system. Path members do, however, validate the contents of a specified path string, and throw an ArgumentException if the string contains characters that are not valid in path strings, as defined in InvalidPathChars.
The members of the Path class enable you to quickly and easily perform common operations such as determining whether a file name extension is part of a path, and combining two strings into one path name.
Note |
---|
In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string. |
In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:
Because all these operations are performed on strings, it is impossible to verify that the results are valid in all scenarios. For example, the GetExtension method parses a string that you pass to it and returns the extension from that string. However, this does not mean that a file with that extension exists on the disk.
The following code example demonstrates some of the main members of the Path class.
Public Sub Main() Const Path1 As String = "c:\temp\MyTest.txt" Const Path2 As String = "c:\temp\MyTest" Const Path3 As String = "temp" If Path.HasExtension(Path1) Then Debug.Print CorString.Format("{0} has an extension.", Path1) End If If Not Path.HasExtension(Path2) Then Debug.Print CorString.Format("{0} has no extension.", Path2) End If If Not Path.IsPathRooted(Path3) Then Debug.Print CorString.Format("The string {0} contains no root information.", Path3) End If Debug.Print CorString.Format("The full path of {0} is {1}.", Path3, Path.GetFullPath(Path3)) Debug.Print CorString.Format("{0} is the location of temporary files.", Path.GetTempPath) Debug.Print CorString.Format("{0} is a file available for use.", Path.GetTempFileName) End Sub ' This example code produces output similar to the following. ' ' c:\temp\MyTest.txt has an extension. ' c:\temp\MyTest has no extension. ' The string temp contains no root information. ' The full path of temp is C:\Program Files (x86)\Microsoft Visual Studio\VB98\temp. ' C:\Users\kelly\AppData\Local\Temp\ is the location of temporary files. ' C:\Users\kelly\AppData\Local\Temp\tmp6B81.tmp is a file available for use.