Get name, type, format and available free space for a drive in CSharp

Description

The following code shows how to get name, type, format and available free space for a drive.

Example


  //from w w  w . j  av  a 2s  .  c  o m

using System;
using System.IO;

static class MainClass
{
    static void Main(string[] args)
    {
        FileInfo file = new FileInfo("c:\\test.txt");

        // Display drive information.
        DriveInfo drv = new DriveInfo(file.FullName);

        Console.Write("Drive: ");
        Console.WriteLine(drv.Name);
        
        if (drv.IsReady)
        {
            Console.Write("Drive type: ");
            Console.WriteLine(drv.DriveType.ToString());
            Console.Write("Drive format: ");
            Console.WriteLine(drv.DriveFormat.ToString());
            Console.Write("Drive free space: ");
            Console.WriteLine(drv.AvailableFreeSpace.ToString());
        }

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip