C# Program to get the name of root directory


Use the RootDirectory property to get the name of the root directory.

Firstly, use DriveInfo to set the drive for which you want the root directory −

DriveInfo dInfo = new DriveInfo("C");

Now, the RootDirectory gives you the result −

dInfo.RootDirectory

Example

Here is the complete code −

using System;
using System.Linq;
using System.IO;
public class Demo {
   public static void Main() {
      DriveInfo dInfo = new DriveInfo("C");
      Console.WriteLine("Root Directory: "+dInfo.RootDirectory);
   }
}

Output

The following is the output −

C:\

Updated on: 22-Jun-2020

471 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements