Drive

In this chapter you will learn:

  1. How to list all drives
  2. Get all logical drives

List all drives

using System;//j  a  va 2s.co  m
using System.IO;

static class MainClass
{
    static void Main(string[] args)
    {
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            try
            {
                Console.WriteLine(
                    "{0} - {1} KB",
                    drive.RootDirectory,
                    drive.AvailableFreeSpace / 1024
                    );
            } catch (IOException) {
                Console.WriteLine(drive);
            }
        }
    }
}

The code above generates the following result.

Get all logical drives

using System;/*ja v  a2  s. c om*/
using System.IO;

class MaionClass
{
  public static void Main(String[] args)
  {
    string[] drives = Directory.GetLogicalDrives();
    Console.WriteLine("Here are your drives:");
    foreach(string s in drives)
    {
      Console.WriteLine("--> {0}", s);
    }

  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. DriveInfo: name, type, format and available free space
Home » C# Tutorial » File, Path
File creation
Deleting Files
Put to recycle bin
File Exists
File moving
File size
File attribute
Set file attribute
File time change
File time
Random file name
Temporary file
Path
Directory creation
Delete directory
Path existance
Change the path
Current Path
Directory listing
Recursive Path Listing
Combine path
Calculate Directory Size
Invalid path characters
System path
Directory copy
Drive
Drive information
File access rule
File System watcher