[英]get removable media drives list in c#
嗨,我需要在c#中檢測所有可移動媒體驅動器ia下拉菜單
任何幫助將不勝感激
謝謝
您可以使用DriveInfo類型來檢索驅動器列表。 您需要檢查DriveType屬性 (枚舉)
var drives = DriveInfo.GetDrives();
foreach (var drive in drives)
{
if (drive.DriveType == DriveType.Removable)
{
Console.WriteLine(drive.Name);
}
}
您還可以使用LINQ-to-Objects查詢驅動器:
var drives = from drive in DriveInfo.GetDrives()
where drive.DriveType == DriveType.Removable
select drive;
foreach(var drive in drives)
{
Console.WriteLine(drive.Name);
}
與提到的@TheCodeKing一樣,您也可以使用WMI查詢驅動器信息。
例如,您可以通過以下方式查詢USB記憶棒:
ManagementObjectCollection drives = new ManagementObjectSearcher(
"SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"
).Get();
如果要使用WMI,請添加對System.Management程序集的引用。
如果要使用此數據填充Windows窗體應用程序中的ComboBox,則需要將結果綁定到ComboBox控件。
例如:
private void Form1_Load(object sender, EventArgs e)
{
var drives = from drive in DriveInfo.GetDrives()
where drive.DriveType == DriveType.Removable
select drive;
comboBox1.DataSource = drives.ToList();
}
概括:
您已使用WMI,請查看此鏈接以獲取信息和示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.