A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Retrieve a list of disk drives using C#This code retrieves a list of drives, stores it into an array of strings, and then loops through it to display the drives. |
On Thursday, October 18th 2007 at 08:09 PM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4 with 3 votes) |
||
|
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsExcellent
how to get only local hard drives, no cd/dvd or mapped network ones?
static void Main(string[] args)
{
Console.Title = "Storm Disk Space Mailer.";
// Store the list of drives into an array of string
string[] DriveList = Environment.GetLogicalDrives();
// Loop through the array
for (int i = 0; i < DriveList.Length; i )
{
// Show each drive
getSize(DriveList[i].Replace(":\\", ""));
}
Console.WriteLine("Listing Complete - Press enter to close.");
Console.ReadLine();
}
public static void getSize(string drive)
{
ManagementObject diskSize = new ManagementObject("win32_logicaldisk.deviceid=\"" drive ":\"");
diskSize.Get();
double GB = Convert.ToDouble(diskSize["FreeSpace"]);
GB = ((((GB / 1024) / 1024) / 1024));
string GBS = GB.ToString();
try
{
GBS = GBS.Substring(0, (GBS.IndexOf(".") 3));
}
catch
{
// Do Nothing
}
string DriveType = diskSize["DriveType"].ToString();
switch (DriveType)
{
case "0":
DriveType = "Unknown";
break;
case "1":
DriveType = "No Root Directory";
break;
case "2":
DriveType = "Removable Disk";
break;
case "3":
DriveType = "Local Disk";
break;
case "4":
DriveType = "Network Drive";
break;
case "5":
DriveType = "Compact Disc";
break;
case "6":
DriveType = "RAM Disk";
break;
default:
DriveType = "Unknown";
break;
}
if (DriveType == "Compact Disc")
{
Console.WriteLine("Drive: " drive " is a CD/DVD Drive - Skipped...");
}
else
{
Console.WriteLine("Drive: " drive " is a " DriveType " with " GBS.ToString() " GB free space.");
}
}
static void Main(string[] args)
{
Console.Title = "Storm Disk Space Mailer.";
// Store the list of drives into an array of string
string[] DriveList = Environment.GetLogicalDrives();
// Loop through the array
for (int i = 0; i < DriveList.Length; i )
{
// Show each drive
getSize(DriveList[i].Replace(":\\", ""));
}
Console.WriteLine("Listing Complete - Press enter to close.");
Console.ReadLine();
}
public static void getSize(string drive)
{
ManagementObject diskSize = new ManagementObject("win32_logicaldisk.deviceid=\"" drive ":\"");
diskSize.Get();
double GB = Convert.ToDouble(diskSize["FreeSpace"]);
GB = ((((GB / 1024) / 1024) / 1024));
string GBS = GB.ToString();
try
{
GBS = GBS.Substring(0, (GBS.IndexOf(".") 3));
}
catch
{
// Do Nothing
}
string DriveType = diskSize["DriveType"].ToString();
switch (DriveType)
{
case "0":
DriveType = "Unknown";
break;
case "1":
DriveType = "No Root Directory";
break;
case "2":
DriveType = "Removable Disk";
break;
case "3":
DriveType = "Local Disk";
break;
case "4":
DriveType = "Network Drive";
break;
case "5":
DriveType = "Compact Disc";
break;
case "6":
DriveType = "RAM Disk";
break;
default:
DriveType = "Unknown";
break;
}
if (DriveType == "Compact Disc")
{
Console.WriteLine("Drive: " drive " is a CD/DVD Drive - Skipped...");
}
else
{
Console.WriteLine("Drive: " drive " is a " DriveType " with " GBS.ToString() " GB free space.");
}
}
Ewps, Double Post :(
Related Source Code
Related Tutorials
C# Job Search