C#.net volume label

  • Thread starter Thread starter Eddie
  • Start date Start date
E

Eddie

Hi,

Can anyone provide an example of getting a hard disk/cdrom volume label in
C#.net?

Thanks in advance,
Eddie
 
See inline--
using System;
using System.Collections;
using System.Management;

namespace HardDriveSample1
{
class HardDrive
{
private string model = null;
private string type = null;
private string serialNo = null;

public string Model
{
get {return model;}
set {model = value;}
}

public string Type
{
get {return type;}
set {type = value;}
}

public string SerialNo
{
get {return serialNo;}
set {serialNo = value;}
}
}

class TestProgram
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ArrayList hdCollection = new ArrayList();

ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach(ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
// hd.Model = wmi_HD["Model"].ToString();
// hd.Type = wmi_HD["InterfaceType"].ToString();

hdCollection.Add(hd);
}

searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
// get the hard drive from collection
// using index
HardDrive hd = (HardDrive)hdCollection;

// get the hardware serial no.
if (wmi_HD["SerialNumber"] == null)
hd.SerialNo = "None";
else
hd.SerialNo = wmi_HD["SerialNumber"].ToString();

++i;
}

// Display available hard drives
foreach(HardDrive hd in hdCollection)
{
Console.WriteLine("Model\t\t: " + hd.Model);
Console.WriteLine("Type\t\t: " + hd.Type);
Console.WriteLine("Serial No.\t: " + hd.SerialNo);
Console.WriteLine();
}

// Pause application
Console.WriteLine("Press [Enter] to exit...");
Console.ReadLine();
}
}
}

--HTH
Peter
 
Back
Top