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



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: