Directory::GetParent Method (String^)
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::DirectoryInfo^The 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.
using namespace System; class Class1 { public: void PrintFileSystemEntries( String^ path ) { try { // Obtain the file system entries in the directory path. array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path ); for ( int i = 0; i < directoryEntries->Length; i++ ) { System::Console::WriteLine( directoryEntries[ i ] ); } } catch ( ArgumentNullException^ ) { System::Console::WriteLine( "Path is a null reference." ); } catch ( System::Security::SecurityException^ ) { System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." ); } catch ( ArgumentException^ ) { System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, \HelloServer' or contains invalid characters." ); } catch ( System::IO::DirectoryNotFoundException^ ) { System::Console::WriteLine( "The path encapsulated in the \HelloServer' Directory object does not exist." ); } } void PrintFileSystemEntries( String^ path, String^ pattern ) { try { // Obtain the file system entries in the directory // path that match the pattern. array<String^>^directoryEntries = System::IO::Directory::GetFileSystemEntries( path, pattern ); for ( int i = 0; i < directoryEntries->Length; i++ ) { System::Console::WriteLine( directoryEntries[ i ] ); } } catch ( ArgumentNullException^ ) { System::Console::WriteLine( "Path is a null reference." ); } catch ( System::Security::SecurityException^ ) { System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." ); } catch ( ArgumentException^ ) { System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, \HelloServer' or contains invalid characters." ); } catch ( System::IO::DirectoryNotFoundException^ ) { System::Console::WriteLine( "The path encapsulated in the \HelloServer' Directory object does not exist." ); } } // Print out all logical drives on the system. void GetLogicalDrives() { try { array<String^>^drives = System::IO::Directory::GetLogicalDrives(); for ( int i = 0; i < drives->Length; i++ ) { System::Console::WriteLine( drives[ i ] ); } } catch ( System::IO::IOException^ ) { System::Console::WriteLine( "An I/O error occurs." ); } catch ( System::Security::SecurityException^ ) { System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." ); } } void GetParent( String^ path ) { try { System::IO::DirectoryInfo^ directoryInfo = System::IO::Directory::GetParent( path ); System::Console::WriteLine( directoryInfo->FullName ); } catch ( ArgumentNullException^ ) { System::Console::WriteLine( "Path is a null reference." ); } catch ( ArgumentException^ ) { System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, or \HelloServer' contains invalid characters." ); } } void Move( String^ sourcePath, String^ destinationPath ) { try { System::IO::Directory::Move( sourcePath, destinationPath ); System::Console::WriteLine( "The directory move is complete." ); } catch ( ArgumentNullException^ ) { System::Console::WriteLine( "Path is a null reference." ); } catch ( System::Security::SecurityException^ ) { System::Console::WriteLine( "The caller does not have the \HelloServer' required permission." ); } catch ( ArgumentException^ ) { System::Console::WriteLine( "Path is an empty String, \HelloServer' contains only white spaces, \HelloServer' or contains invalid characters." ); } catch ( System::IO::IOException^ ) { System::Console::WriteLine( "An attempt was made to move a \HelloServer' directory to a different \HelloServer' volume, or destDirName \HelloServer' already exists." ); } } }; int main() { Class1 * snippets = new Class1; String^ path = System::IO::Directory::GetCurrentDirectory(); String^ filter = "*.exe"; snippets->PrintFileSystemEntries( path ); snippets->PrintFileSystemEntries( path, filter ); snippets->GetLogicalDrives(); snippets->GetParent( path ); snippets->Move( "C:\\proof", "C:\\Temp" ); return 0; }
for reading from files or directories. Associated enumeration: FileIOPermissionAccess::Read
Available since 10
.NET Framework
Available since 1.1