C# Directory GetParent

Description

Directory GetParent Retrieves the parent directory of the specified path, including both absolute and relative paths.

Syntax

Directory.GetParent has the following syntax.


public static DirectoryInfo GetParent(
  string path
)

Parameters

Directory.GetParent has the following parameters.

  • path - The path for which to retrieve the parent directory.

Returns

Directory.GetParent method returns The parent directory, or null if path is the root directory, including the root of a UNC server or share name.

Example

The following example demonstrates how to use the GetParent method.


/*from  www  .j a va  2  s .co m*/
using System;

class Class1 
{
    static void Main(string[] args) 
    {
        System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(".");

        System.Console.WriteLine(directoryInfo.FullName);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter