Skip to content

Instantly share code, notes, and snippets.

@AlexP11223
Last active March 29, 2017 18:25
Show Gist options
  • Save AlexP11223/5f22d04d7dc621c4f6e8f3847dfe10d4 to your computer and use it in GitHub Desktop.
Save AlexP11223/5f22d04d7dc621c4f6e8f3847dfe10d4 to your computer and use it in GitHub Desktop.
Simple example showing how to load disks information using WMI. C#, .NET
using System;
using System.Collections.Generic;
namespace WindowsFormsApplication19
{
class Drive
{
public string DeviceId { get; set; }
public string Model { get; set; }
public string Serial { get; set; }
public UInt64 Size { get; set; }
public List<LogicalDisk> LogicalDisks { get; set; }
public Drive()
{
LogicalDisks = new List<LogicalDisk>();
}
public override string ToString()
{
return string.Format("DeviceID: {0}, Model: {1}, Serial: {2}, Size: {3}, LogicalDisks: [{4}]",
DeviceId, Model, Serial, Size, String.Join("; ", LogicalDisks));
}
}
class LogicalDisk
{
public string Id { get; set; }
public string Name { get; set; }
public UInt64 Size { get; set; }
public override string ToString()
{
return string.Format("{0} ({1}), {2}", Id, Name, Size);
}
}
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Management;
namespace WindowsFormsApplication19
{
class DriveService
{
public List<Drive> LoadDrives()
{
var drives = new List<Drive>();
using (var managementClass = new ManagementClass("Win32_DiskDrive"))
{
var driveCollection = managementClass.GetInstances();
foreach (var driveMo in driveCollection)
{
var drive = new Drive
{
DeviceId = (string) driveMo["DeviceID"],
Model = (string) driveMo["Model"],
Serial = (string) driveMo["SerialNumber"],
Size = (UInt64) driveMo["Size"]
};
drive.LogicalDisks = LoadLogicalDisks(drive.DeviceId);
drives.Add(drive);
}
}
return drives;
}
public List<LogicalDisk> LoadLogicalDisks(string deviceId)
{
var logicalDisks = new List<LogicalDisk>();
using (var searcher1 = new ManagementObjectSearcher(
String.Format("ASSOCIATORS OF {{Win32_DiskDrive.DeviceID=\"{0}\"}} WHERE AssocClass = Win32_DiskDriveToDiskPartition",
deviceId.Replace("\\", "\\\\"))))
{
var partitionsCollection = searcher1.Get();
foreach (var partitionMo in partitionsCollection)
{
string partitionId = (string) partitionMo["DeviceID"];
using (var searcher2 = new ManagementObjectSearcher(
String.Format("ASSOCIATORS OF {{Win32_DiskPartition.DeviceID=\"{0}\"}} WHERE AssocClass = Win32_LogicalDiskToPartition",
partitionId)))
{
var logicalDiskCollection = searcher2.Get();
foreach (var logicalDiskMo in logicalDiskCollection)
{
var logicalDisk = new LogicalDisk
{
Id = (string)logicalDiskMo["DeviceID"],
Name = (string)logicalDiskMo["VolumeName"],
Size = (UInt64)logicalDiskMo["Size"]
};
logicalDisks.Add(logicalDisk);
}
}
}
}
return logicalDisks;
}
public void SaveDrives(IEnumerable<Drive> drives)
{
foreach (var drive in drives)
{
SaveDrive(drive);
}
}
public void SaveDrive(Drive drive)
{
using (var connection = new SqlConnection("..."))
{
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "INSERT INTO ... (model, ...) VALUES (@Model, ...)";
cmd.Parameters.AddWithValue("@Model", drive.Model);
// ...
cmd.Prepare();
cmd.ExecuteNonQuery();
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication19
{
public partial class Form1 : Form
{
private List<Drive> _drives;
private readonly DriveService _driveService = new DriveService();
public Form1()
{
InitializeComponent();
}
private void btnLoadDrives_Click(object sender, EventArgs e)
{
try
{
_drives = _driveService.LoadDrives();
ShowDrives(_drives);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
_driveService.SaveDrives(_drives);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ShowDrives(List<Drive> drives)
{
listBox1.Items.Clear();
foreach (var drive in drives)
{
listBox1.Items.Add(String.Format("{0}: {1}",
drive.Model,
String.Join(", ", drive.LogicalDisks.Select(l => String.Format("{0} ({1})", l.Id, l.Name)))));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment