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
ASP.NET Web Application Message Box
Passing Array of Strings to Webmethod with Variable Number of Arguments Using Jquery Ajax
Changing Font for Richtextbox Without Losing Formatting
How to Draw Shapes in Winforms
When Would You Use Delegates in C#
How to Prevent Synchronous Continuations on a Task
Custom Path of the User.Config
How to Get Some Values from a JSON String in C#
Casting VS Converting an Object Tostring, When Object Really Is a String
Can the Oracle Managed Driver Use Async/Await Properly
Writing String at the Same Position Using Console.Write in C# 2.0
How to Get MAC Address of Client That Browse Web Site by ASP.NET MVC C#
Converting ASP.NET MVC Razor @Helper Function into a Method of a Helper Class
Refactoring Code to Avoid Anti-Pattern
When Should I Choose Inheritance Over an Interface When Designing C# Class Libraries