C# Path GetDirectoryName

Description

Path GetDirectoryName Returns the directory information for the specified path string.

Syntax

Path.GetDirectoryName has the following syntax.


public static string GetDirectoryName(
  string path
)

Parameters

Path.GetDirectoryName has the following parameters.

  • path - The path of a file or directory.

Returns

Path.GetDirectoryName method returns Directory information for path, or null if path denotes a root directory or is null. Returns String.Empty if path does not contain directory information.

Example

The following code example demonstrates using the GetDirectoryName method.


using System.IO;//from   ww w .j  ava2 s . c o  m
using System;
public class MainClass{
  public static void Main(String[] argv){  
    string filePath = @"C:\MyDir\MySubDir\myfile.ext";
    string directoryName = Path.GetDirectoryName(filePath);
    Console.WriteLine(directoryName);

  }
}
    

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