Home » C# » بدست آوردن مشخصات فلش مموری در C# Finding a USB mass storage devices “disk letter” by its PID and VID number or name

بدست آوردن مشخصات فلش مموری در C# Finding a USB mass storage devices “disk letter” by its PID and VID number or name

void P_Add(string str_Input)
txt_new_Temp.Text += Environment.NewLine + str_Input;

private void button2_Click(object sender, EventArgs e)
ManagementObjectSearcher diskDrives = new ManagementObjectSearcher(“SELECT * FROM Win32_DiskDrive WHERE InterfaceType=’USB'”);
foreach (ManagementObject diskDrive in diskDrives.Get())
string DeviceID = diskDrive[“DeviceID”].ToString();
string DriveLetter = “”;
string DriveDescription = “”;

// associate physical disks with partitions
ManagementObjectSearcher partitionSearcher = new ManagementObjectSearcher(String.Format(
“associators of {{Win32_DiskDrive.DeviceID='{0}’}} where AssocClass = Win32_DiskDriveToDiskPartition”,

foreach (ManagementObject partition in partitionSearcher.Get())
// associate partitions with logical disks (drive letter volumes)
ManagementObjectSearcher logicalSearcher = new ManagementObjectSearcher(String.Format(
“associators of {{Win32_DiskPartition.DeviceID='{0}’}} where AssocClass = Win32_LogicalDiskToPartition”,

foreach (ManagementObject logical in logicalSearcher.Get())
// finally find the logical disk entry to determine the volume name
ManagementObjectSearcher volumeSearcher = new ManagementObjectSearcher(String.Format(
“select * from Win32_LogicalDisk where Name='{0}'”,

foreach (ManagementObject volume in volumeSearcher.Get())
DriveLetter = volume[“Name”].ToString();
if (volume[“VolumeName”] != null)
DriveDescription = volume[“VolumeName”].ToString();

char VolumeLetter = DriveLetter[0];
string VolumeName = DriveDescription;
string Manufacturer = (string)diskDrive[“Manufacturer”];
string MediaType = (string)diskDrive[“MediaType”];
string Model = (string)diskDrive[“Model”];
string SerialNumber = (string)diskDrive[“SerialNumber”];
long Size = Convert.ToInt64(volume[“Size”]);
long FreeSpace = Convert.ToInt64(volume[“FreeSpace”]);

P_Add(“VolumeLetter= ” + VolumeLetter.ToString());
P_Add(“VolumeName= ” + VolumeName);
P_Add(“Manufacturer= ” + Manufacturer);
P_Add(“MediaType= ” + MediaType);
P_Add(“Model= ” + Model);
P_Add(“SerialNumber= ” + SerialNumber);
P_Add(“Size= ” + Size.ToString());
P_Add(“FreeSpace= ” + FreeSpace.ToString());
P_Add(” “);


Check Also

شرط یک خطی در سی شارپ single line IF syntax in c#

Single line if statement with 2 actions

شرط یک خطی در c# Sounds like you really want a Dictionary<int, string> or possibly a switch statement… You can do …

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *