Get a List of Fixed, Network, USB and CDROM Drives on your Machine using C# or VB.NET

The DriveInfo.DriveType enum is very useful to determine drive type. Let us see how to list down all USB drives connected to your machine using C# or VB.NET

C#

using System;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo di in drives)
{
if (di.IsReady)
{
Console.WriteLine("Volume label: {0} ", di.VolumeLabel);
Console.WriteLine("Drive Type: {0} ", di.DriveType);
Console.WriteLine("Free space: {0} bytes ", di.TotalFreeSpace);
Console.WriteLine("Drive Size: {0} bytes \n", di.TotalSize);
}
}
Console.ReadLine();
}
catch (Exception ex)
{
// handle ex
}
}
}
}

VB.NET

Imports System
Imports System.IO

Namespace ConsoleApplication1
Friend Class Program
Shared Sub Main(ByVal args() As String)
Try
Dim
drives() As DriveInfo = DriveInfo.GetDrives()
For Each di As DriveInfo In drives
If di.IsReady Then
Console.WriteLine("Volume label: {0} ", di.VolumeLabel)
Console.WriteLine("Drive Type: {0} ", di.DriveType)
Console.WriteLine("Free space: {0} bytes ", di.TotalFreeSpace)
Console.WriteLine("Drive Size: {0} bytes ", di.TotalSize)
End If
Next
di
Console.ReadLine()
Catch ex As Exception
' handle ex
End Try
End Sub
End Class
End Namespace



OUTPUT

image






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: