Displaying drive information
I am new to C#. I am not able to retreive information about all drives in my laptop. I used following code.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IOEx
{
class DriveList
{
static void Main()
{
foreach (DriveInfo drvInfo in DriveInfo.GetDrives())
{
try
{
//if (drvInfo.IsReady)
{
Console.WriteLine("Drive {0} ({1}), free space {2:#,#} GB Total space ({3: #,#}) GB", drvInfo.Name, drvInfo.VolumeLabel, drvInfo.TotalFreeSpace, drvInfo.TotalSize);
}
}
catch(Exception ex)
{
Console.WriteLine("Application generated exception : "+ex.Message);
}
Console.ReadKey();
}
}
}
}
the laptop has hard disk with two partitions and a CD rom. The output displayed shows information about C drive only that too considering entire physical drive. program do not detect CD rom. Can somebody explain.
Amit ChoudharyPosted Mar 9, 2010, 1:14 AM
Please mark my answer and help me back.
Rajiv YelkawarPosted Mar 10, 2010, 12:29 AM
Rajiv YelkawarPosted Mar 6, 2010, 4:56 AM
Thanks Amit
You have also solved my other problem of conversion into GB. Thanks again
Amit ChoudharyPosted Mar 6, 2010, 12:35 AM
i have modified your code and its working fine in my system. and it is showing only c drive cause you have put Console.ReadKey() inside the loop. so it wait for key press from key board to go next.
see the code below showing all drives.
static void Main(string[] args)
{
foreach (DriveInfo drvInfo in DriveInfo.GetDrives())
{
try
{
//if (drvInfo.IsReady)
{
Console.WriteLine("Drive {0} ({1}), free space {2:#,#} GB Total space ({3: #,#}) GB", drvInfo.Name, drvInfo.VolumeLabel, (drvInfo.TotalFreeSpace) / (long)Math.Pow(1000, 3), (drvInfo.TotalSize) / (long)Math.Pow(1000, 3));
}
}
catch (Exception e)
{
Console.WriteLine("OtherDrives : " + e.Message);
}
}
Console.ReadKey();
}
Please mark the answer if it helps you.