Reader Level:
Articles

Get Parent Directory in C#

By Mahesh Chand on Jul 18, 2012
How to get the parent directory of a sub directory using C# and .NET.
  • 0
  • 0
  • 16917

We can use the System.IO.DirectoryInfo class to get a parent of a specified folder or directory. 

You must import the System.IO before you can use this class.

using System.IO;

The Parent property of DirectoryInfo returns the parent directory as a DirectoryInfo. We can use the FullName property to get the full name of the directory. 

The following code snippet does the same.

string root = @"C:\Temp\Mahesh";
DirectoryInfo di = new DirectoryInfo(root);
Console.WriteLine(di.Parent.FullName);

Download free book: Working with Directories in C#

Mahesh Chand

C# Corner founder, 10-times Microsoft MVP, technical author, and software architect. Mahesh authored his first book, A Programmer’s Guide to ADO.NET in C# with APress at the age of 25. C# Corner is a memb... Read more

COMMENT USING