Directory.GetParent Method (String)
Note |
|---|
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience. |
Retrieves the parent directory of the specified path, including both absolute and relative paths.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- path
-
Type:
System.String
The path for which to retrieve the parent directory.
Return Value
Type: System.IO.DirectoryInfoThe parent directory, or null if path is the root directory, including the root of a UNC server or share name.
| Exception | Condition |
|---|---|
| IOException | The directory specified by path is read-only. |
| UnauthorizedAccessException | The caller does not have the required permission. |
| ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters with the GetInvalidPathChars method. |
| ArgumentNullException | path is null. |
| PathTooLongException | The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters. |
| DirectoryNotFoundException | The specified path was not found. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
Trailing spaces are removed from the end of the path parameter before getting the directory.
The string returned by this method consists of all characters in the path up to, but not including, the last DirectorySeparatorChar or AltDirectorySeparatorChar. For example, passing the path "C:\Directory\SubDirectory\test.txt" to GetParent returns "C:\Directory\SubDirectory". Passing "C:\Directory\SubDirectory" returns "C:\Directory". However, passing "C:\Directory\SubDirectory\" returns "C:\Directory\SubDirectory", because the ending directory separator is after "SubDirectory".
The path parameter is not case-sensitive.
For a list of common I/O tasks, see Common I-O Tasks.
The following example demonstrates how to use the GetParent method to retrieve the parent directory of a user-specified location, "path". The value returned by the GetParent method is then printed to the console. The example is configured to catch all errors common to this method.
Option Explicit On Option Strict On Imports System Namespace GetFileSystemEntries Class Class1 Overloads Shared Sub Main(ByVal args() As String) Dim snippets As New Class1() Dim path As String = System.IO.Directory.GetCurrentDirectory() Dim filter As String = "*.exe" snippets.PrintFileSystemEntries(path) snippets.PrintFileSystemEntries(path, filter) snippets.GetLogicalDrives() snippets.GetParent(path) snippets.Move("C:\proof", "C:\Temp") End Sub 'Main Sub PrintFileSystemEntries(ByVal path As String) Try ' Obtain the file system entries in the directory path. Dim directoryEntries As String() directoryEntries = System.IO.Directory.GetFileSystemEntries(path) Dim str As String For Each str In directoryEntries System.Console.WriteLine(str) Next str Catch exp As ArgumentNullException System.Console.WriteLine("Path is a null reference.") Catch exp As System.Security.SecurityException System.Console.WriteLine("The caller does not have the " + _ "required permission.") Catch exp As ArgumentException System.Console.WriteLine("Path is an empty string, " + _ "contains only white spaces, " + _ "or contains invalid characters.") Catch exp As System.IO.DirectoryNotFoundException System.Console.WriteLine("The path encapsulated in the " + _ "Directory object does not exist.") End Try End Sub Sub PrintFileSystemEntries(ByVal path As String, _ ByVal pattern As String) Try ' Obtain the file system entries in the directory ' path that match the pattern. Dim directoryEntries As String() directoryEntries = _ System.IO.Directory.GetFileSystemEntries(path, pattern) Dim str As String For Each str In directoryEntries System.Console.WriteLine(str) Next str Catch exp As ArgumentNullException System.Console.WriteLine("Path is a null reference.") Catch exp As System.Security.SecurityException System.Console.WriteLine("The caller does not have the " + _ "required permission.") Catch exp As ArgumentException System.Console.WriteLine("Path is an empty string, " + _ "contains only white spaces, " + _ "or contains invalid characters.") Catch exp As System.IO.DirectoryNotFoundException System.Console.WriteLine("The path encapsulated in the " + _ "Directory object does not exist.") End Try End Sub ' Print out all logical drives on the system. Sub GetLogicalDrives() Try Dim drives As String() drives = System.IO.Directory.GetLogicalDrives() Dim str As String For Each str In drives System.Console.WriteLine(str) Next str Catch exp As System.IO.IOException System.Console.WriteLine("An I/O error occurs.") Catch exp As System.Security.SecurityException System.Console.WriteLine("The caller does not have the " + _ "required permission.") End Try End Sub Sub GetParent(ByVal path As String) Try Dim directoryInfo As System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(path) System.Console.WriteLine(directoryInfo.FullName) Catch exp As ArgumentNullException System.Console.WriteLine("Path is a null reference.") Catch exp As ArgumentException System.Console.WriteLine("Path is an empty string, " + _ "contains only white spaces, or " + _ "contains invalid characters.") End Try End Sub Sub Move(ByVal sourcePath As String, ByVal destinationPath As String) Try System.IO.Directory.Move(sourcePath, destinationPath) System.Console.WriteLine("The directory move is complete.") Catch exp As ArgumentNullException System.Console.WriteLine("Path is a null reference.") Catch exp As System.Security.SecurityException System.Console.WriteLine("The caller does not have the " + _ "required permission.") Catch exp As ArgumentException System.Console.WriteLine("Path is an empty string, " + _ "contains only white spaces, " + _ "or contains invalid characters.") Catch exp As System.IO.IOException System.Console.WriteLine("An attempt was made to move a " + _ "directory to a different " + _ "volume, or destDirName " + _ "already exists.") End Try End Sub End Class End Namespace
for reading from files or directories. Associated enumeration: FileIOPermissionAccess.Read
Available since 10
.NET Framework
Available since 1.1
