Tips n Tracks

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color

Reference

Sample image

Microsoft .NET Framework Get Details.

Sample image

Microsoft .NET Framework Get Details.

Reference

Sample image Microsoft .NET Framework Get Details.
Sample image

Microsoft .NET Framework Get Details.


List All System Drives using C#.NET


List All System Drives using C#.NET

This tip will explain how to get the computer’s logical drives from the local system. The code will work on flavors of C#.net. First of Create new C#.Net Console Application, you can do same by pressing Ctrl+Shift+N

Create new projet

Now replace you Program.cs file content with following…

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;  
 
namespace DirList

 
class Test
{
public static void Main()
{
DriveInfo[] allDrives = DriveInfo.GetDrives(); 
 
foreach (DriveInfo dirInfo in allDrives)
{
Console.WriteLine("Drive {0}", dirInfo.Name);
Console.WriteLine("File type: {0}", dirInfo.DriveType);
if (dirInfo.IsReady == true)
{
Console.WriteLine("Volume label: {0}", dirInfo.VolumeLabel);
Console.WriteLine("File system: {0}", dirInfo.DriveFormat);
Console.WriteLine("Available space:{0, 15} bytes", dirInfo.AvailableFreeSpace); 
 
Console.WriteLine("Total available space: {0, 15} bytes", dirInfo.TotalFreeSpace); 
 
Console.WriteLine("Total size:{0, 15} bytes ",dirInfo.TotalSize);
Console.WriteLine("\n"); 
 
}
}
Console.Read();
}

 
}

Which will shows us following output. it also show information about your Network drive and CD/DVD drive too…

all system drives listed with c#.net

To get all directory information we have use System.IO namespace. 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.

Then using DriveInfo class, we get information on drive. This class models a drive and provides methods and properties to query for drive information. Use DriveInfo to determine what drives are available, and what type of drives they are. You can also query to determine the capacity and available free space on the drive.

Following properties are supported by DriveInfo Public Properties

DriveInfo Public Properties
Name Description
AvailableFreeSpace Indicates the amount of available free space on a drive.
DriveFormat Give the name of the file system, such as NTFS or FAT32.
DriveType Give the drive type.
IsReady Give a value indicating whether a drive is ready.
Name Give the name of a drive.
RootDirectory Give the root directory of a drive.
TotalFreeSpace Give the total amount of free space available on a drive.
TotalSize Give the total size of storage space on a drive.
VolumeLabel Give or sets the volume label of a drive.

Download

Download source code for ‘List All System Drives in C#.NET’

Download

Chetan love blogging. He regularly blogs at http://www.tipsntracks.com. You can connect with Chetan on Twitter, Facebook and Google Plus...

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

super example

Comment by arumugaselvam on January 7, 2011 @ 9:57 am

Leave a comment

(required)

(required)

*
To prove that you're not a bot, enter this code
Anti-Spam Image