I want to retrieve the list of fixed disks in a system. But C#s GetDrives Fixed Drives are including plug USB Harddisks.
Any idea how I may detect that a fixed Drive is not an USB harddisk or vica versa?
I want to retrieve the list of fixed disks in a system. But C#s GetDrives Fixed Drives are including plug USB Harddisks.
Any idea how I may detect that a fixed Drive is not an USB harddisk or vica versa?
Solution nicked from How to get serial number of USB-Stick in C# :
//import the System.Management namespace at the top in your "using" statement.
ManagementObjectSearch theSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
InterfaceType='USB'
, and why it doesn't matter that sticks are included. –
Savitt use DriveType to detect the type of the drive:
using System.IO;
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady && d.DriveType == DriveType.Fixed)
{
// This is the drive you want...
}
}
EDIT1:
check the following link: How do I detected whether a hard drive is connected via USB?
Here you can get list of USB hard disk.
//Add Reference System.Management and use namespace at the top of the code.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
foreach (ManagementObject queryObj in searcher.Get())
{
foreach (ManagementObject b in queryObj.GetRelated("Win32_DiskPartition"))
{
foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
{
Console.WriteLine(String.Format("{0}" + "\\", c["Name"].ToString())); // here it will print USB drive letter
}
}
}
Here you can get list of all fixed drives(System and USB hard disks):
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady == true && d.DriveType == DriveType.Fixed)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" Drive type: {0}", d.DriveType);
}
}
If you compare them,then you can retrieve the list of fixed disks in a system but without USB hard disks.
Use this MSDN link for individual solutions (including finding drive letters): WMI Tasks: Disks and File Systems
© 2022 - 2024 — McMap. All rights reserved.