×

C#.NET System.IO

System.IO in C# - Get Drive Information Using DriveInfo

This program demonstrates the System.IO namespace and uses the DriveInfo class to retrieve information about the drives available on the system. It displays the drive name, drive type and, when a drive is ready, its volume label, file system, available free space, total free space and total size.

Beginner 30 Minutes Monopoly IT Solutions Updated 2026-08-17
.NET 10 class training banner
5.0
5.0
4.6

10 AM Class

9 AM Class

Overview

What you'll cover

Learning objectives

  • Understand the purpose of the System.IO namespace.
  • Learn the System.IO classes listed in the source comments.
  • Use the DriveInfo class.
  • Retrieve all available drives using DriveInfo.GetDrives().
  • Use a foreach loop to process each drive.
  • Check whether a drive is ready using IsReady.
  • Display drive name and drive type.
  • Display volume label and file system information.
  • Calculate and display storage values in GB.

Prerequisites

  • C# Basics
  • Namespaces
  • Classes and Objects
  • Arrays
  • foreach Loop
  • if Statement
  • Console Output
  • Arithmetic Operators

Related topics

  • System.IO
  • DriveInfo
  • Directory
  • DirectoryInfo
  • File
  • FileInfo
  • FileStream
  • StreamWriter
  • StreamReader
  • BinaryWriter
  • BinaryReader
  • foreach Loop
Theory

Concept breakdown

System.IO

The source comments state that the System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

Classes Listed in System.IO Comments

The source comments list the following classes: DriveInfo, Directory, DirectoryInfo, File, FileInfo, FileStream, StreamWriter, StreamReader, BinaryWriter and BinaryReader.

DriveInfo Class

This program uses the DriveInfo class to work with drive information.

GetDrives()

DriveInfo.GetDrives() is used in the program to retrieve the drives and store them in a DriveInfo array named drives.

foreach Loop

The foreach loop processes each DriveInfo object from the drives array one by one.

Drive Name

drive.Name is used to display the name of the current drive.

Drive Type

drive.DriveType is used to display the type of the current drive.

IsReady Check

The program checks drive.IsReady before accessing and displaying additional drive information.

Volume Label

When the drive is ready, drive.VolumeLabel is displayed.

File System

When the drive is ready, drive.DriveFormat is displayed as the file system.

Storage Space Conversion

The program divides AvailableFreeSpace, TotalFreeSpace and TotalSize by 1024 three times to display the values in GB.

Syntax
Hands-on example

Display Drive Information Using DriveInfo

Console Output
Walkthrough

Step by step

  1. 1

    Retrieve the Drives

    DriveInfo.GetDrives() retrieves the drives and stores them in the drives array.

  2. 2

    Start the foreach Loop

    The foreach loop processes each DriveInfo object in the drives array.

  3. 3

    Display the Drive Name

    drive.Name is displayed for the current drive.

  4. 4

    Display the Drive Type

    drive.DriveType is displayed for the current drive.

  5. 5

    Check Whether the Drive Is Ready

    The if condition checks drive.IsReady before displaying additional information.

  6. 6

    Display the Volume Label

    When the drive is ready, drive.VolumeLabel is displayed.

  7. 7

    Display the File System

    When the drive is ready, drive.DriveFormat is displayed.

  8. 8

    Display Available Free Space

    drive.AvailableFreeSpace is divided by 1024 three times and displayed in GB.

  9. 9

    Display Total Free Space

    drive.TotalFreeSpace is divided by 1024 three times and displayed in GB.

  10. 10

    Display Total Size

    drive.TotalSize is divided by 1024 three times and displayed in GB.

  11. 11

    Process the Next Drive

    The foreach loop continues until all drives in the drives array have been processed.

Summary

Key takeaways

Key points

  • System.IO is the namespace described in the source comments for file, data stream, file and directory support.
  • The source comments list ten System.IO classes.
  • This program uses the DriveInfo class.
  • DriveInfo.GetDrives() retrieves the drives used by the program.
  • A foreach loop processes each drive.
  • drive.Name displays the drive name.
  • drive.DriveType displays the drive type.
  • drive.IsReady is checked before displaying detailed drive information.
  • drive.VolumeLabel displays the volume label.
  • drive.DriveFormat displays the file system.
  • AvailableFreeSpace, TotalFreeSpace and TotalSize are converted to GB by dividing by 1024 three times.

Advantages

  • Retrieves and displays information for all drives returned by DriveInfo.GetDrives().
  • Uses a foreach loop for simple processing of the DriveInfo array.
  • Checks IsReady before accessing detailed drive information.
  • Displays multiple drive properties in one program.
  • Converts storage values to GB for readable output.

Disadvantages

Real-world use

System Storage Information

This program demonstrates how an application can enumerate available drives and display storage-related information such as drive name, type, file system and free or total space.

Interview prep

Frequently asked questions

System.IO.

Types for reading and writing to files and data streams, and types that provide basic file and directory support.

DriveInfo.

By using DriveInfo.GetDrives().

A foreach loop.

drive.Name.

drive.DriveType.

It checks whether the drive is ready before displaying the additional drive information inside the if block.

drive.VolumeLabel.

drive.DriveFormat.

It divides the storage value by 1024, then 1024, then 1024.

Test yourself

Quick MCQ practice

1. Which namespace is used in this topic?

2. Which class is used to retrieve drive information?

3. Which method retrieves the drives?

4. Which loop is used to process each drive?

5. Which property displays the drive name?

6. Which property is checked before displaying detailed information?

7. Which property displays the volume label?

8. Which property is displayed as the file system?

9. Which value is used to display available free storage?

10. How many System.IO classes are listed in the source comments?

Apply it

Practice on your own

Coding exercise

Display System Drive Information

Write a C# program using DriveInfo.GetDrives() to retrieve the drives, process each drive using a foreach loop and display its name and drive type. When the drive is ready, display its volume label, file system, available free space, total free space and total size.

Assignment

  1. Write a program to display the names of all drives using DriveInfo.GetDrives().
  2. Write a program to display the name and drive type of each drive.
  3. Write a program to display detailed information only when drive.IsReady is true.
  4. Write a program to display volume label and file system for ready drives.
  5. Write a program to display available free space and total size in GB.

Tags

#csharp#dotnet#system-io#driveinfo#getdrives#drives#file-system#beginner

References

getintouch

Book for Live Demo!