c# 判断有没有U盘

2026年09月20日 01:53
有3个网友回答
网友(1):

同意一楼,首先添加引用System.Management
System.Management.SelectQuery selectQuery = new System.Management.SelectQuery("select * from win32_logicaldisk where DriveType='2'");
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(selectQuery);
if (searcher.Get().Count == 1)
{//有移动硬盘
}

网友(2):

DriveInfo[] s= DriveInfo.GetDrives();
foreach (DriveInfo i in s)
{
if (i.DriveType == DriveType.Removable)
{
MessageBox.Show("发现U盘或移动硬盘");
}
}

网友(3):

我是来学习的