How do I detect a USB drive letter from a c# application?

為{幸葍}努か 提交于 2019-12-12 12:38:04

问题


How to detect the USB drive letter from c# program which is not residing in the USB? The program should reside in the system, if multiple USB's are connected then i should first able to get the manufacturer name also.


回答1:


This will get all of the removable drives attached (including USB drives):

foreach (DriveInfo drive in DriveInfo.GetDrives())
{
    if (drive.DriveType == DriveType.Removable)
    {
        // Code here
    }
}

Getting the USB drive manufacturer may be more difficult, and you may need to use WMI.

Edit: Here are 2 links on reading USB drive information:

  • how to determine USB Flash drive manufacturer?
  • Finding serial number of USB drive without WMI



回答2:


List<string> list_usb= DriveInfo.GetDrives().Where(d => d.DriveType.ToString() == "Removable").Select(d => d.Name).ToList();

            foreach(var i in list_usb)
            {
                Console.WriteLine(i);
            }

you can try this.



来源:https://stackoverflow.com/questions/1273872/how-do-i-detect-a-usb-drive-letter-from-a-c-sharp-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!