ADVERTISEMENT
ADVERTISEMENT

C# program to read the list of available disk drives

Here, we are going to learn how to read the list of available disk drives in C#?
Submitted by Nidhi, on September 17, 2020

Here we will read the list of available disk drives using DriveInfo class and print the drives name, drive type, and size of the drive on the console screen.

Program:

The source code to read the list of available disk drives is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to read the list of available disk drives.

using System.IO;
using System;

class Demo
{
    static void ReadDrives()
    {
        DriveInfo[] drvList = DriveInfo.GetDrives();

        foreach (DriveInfo drv in drvList)
        {
            Console.WriteLine("Drive Name: "+ drv.Name      );
            Console.WriteLine("\tDrive Type: "+ drv.DriveType );
            
            if (drv.IsReady == true)
            {
                long size=0;
                size = (drv.TotalSize) / (1024*1024*1024);
                Console.WriteLine("\tSize of drive in GB "+size +"\n");    
            }
        }
    }
    public static void Main()
    {
        ReadDrives();
    }
}

Output:

Drive Name: C:\
        Drive Type: Fixed
        Size of drive in GB 50

Drive Name: D:\
        Drive Type: Fixed
        Size of drive in GB 97

Drive Name: E:\
        Drive Type: Fixed
        Size of drive in GB 97

Drive Name: F:\
        Drive Type: CDRom
Drive Name: H:\
        Drive Type: Removable
Press any key to continue . . .

Explanation:

In the above program, we created a class Demo that contains two static methods ReadDrives() and Main().

In the ReadDrives() class we read available disk drives and print the disk name, disk type, and size of the disk in GigaBytes on the console screen using DriveInfo class. 

The Main() method is the entry point of the program, here we called the static method ReadDrives().

C# Files Programs »


ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.