How to Retrieve Disk Information in C#

How do I retrieve disk information in C#?

For most information, you can use the DriveInfo class.

using System;
using System.IO;

class Info {
public static void Main() {
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives) {
//There are more attributes you can use.
//Check the MSDN link for a complete example.
Console.WriteLine(drive.Name);
if (drive.IsReady) Console.WriteLine(drive.TotalSize);
}
}
}

using c# how can I extract information about the hard drives present on the local machine

You can use WMI Calls to access info about the hard disks.

//Requires using System.Management; & System.Management.dll Reference

ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); 
disk.Get();
Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
Console.WriteLine("Logical Disk FreeSpace = " + disk["FreeSpace"] + "bytes");

How can I retrieve information about disk volumes?

The Win32 API is the (apparently only) way to go here, Virtual Disk Service is the magic word.

Here is a good example in C++ that will get you started. The number of different COM interfaces was pretty confusing for me at first, but the How Virtual Disk Service Works article was of great help getting the big picture.

It's actually pretty easy. Despite never having done any serious C++ coding and never having even touched COM before, I was still able to get the basic functionality to work in a few hours.

Getting a list of logical drives

System.IO.DriveInfo.GetDrives()

How do I get disk utilization info in C#

Use this code in a timer_tick method:

    PerformanceCounter disk = new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
Int32 j = 0;
j = Convert.ToInt32(disk.NextValue());
Console.WriteLine(j);

The code is in C#

See disk management info with c#

You can use WMI to retrieve that information

System.Management.ManagementObject("Win32_LogicalDisk.DeviceID=" & DriveLetter & ":")

See more at Win32_LogicalDisk class I hope it helps. By the way there is PInvoke too GetVolumeInformation.

If you need 'PHYSICALDRIVE0' you should use Win32_PhysicalMedia class and the class Win32_DiskDrivePhysicalMedia glue both.

An exemple of your need in C#

public string GetDiskNumber(string letter)
{
var ret = "0";
var scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
var query = new ObjectQuery("Associators of {Win32_LogicalDisk.DeviceID='" + letter + ":'} WHERE ResultRole=Antecedent");
var searcher = new ManagementObjectSearcher(scope, query);
var queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
ret = m["Name"].ToString().Replace("Disk #", "")[0].ToString();
}
return ret;
}


Related Topics



Leave a reply



Submit