Login(Email) Password Forgot Password Account Settings
Home ASP.net System Info C# Books Java Script Visual C++(MFC) C/C++ Win API Java Contact Us
Path class GetDirectoryName method in C#.net in Visual Studio 2008
Returns all the directory information, which is the text between the first and last directory separators (\). (How to get (extract) Directory name  from a path in C#.net Visual Studio 2008)

Syntax:

public static string GetDirectoryName(string path)

Example:

using System; 
using System.IO;
public class CGetDir{ 
public static void Main() { 
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string rootPath = @"C:\";
string directoryName;

directoryName = Path.GetDirectoryName(fileName);

// directoryName will conatin 'C:\mydir' directoryName = Path.GetDirectoryName(path);
// directoryName will conatin 'C:\mydir' directoryName = Path.GetDirectoryName(rootPath); // directoryName will conatin '' Note:If the path consists of a root directory, such as "c:\", null is return

}

}