概述
1、使用.net管理对象(引入System.Management)
public static List<string> getDisk()
{
WqlObjectQuery wmiquery = new WqlObjectQuery("select * from Win32_LogiCalDisk");
ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery);
ManagementObjectCollection queryCollection = wmifind.Get();
List<string> ls = new List<string>();
foreach (var disk in queryCollection)
{
ls.Add(disk["DeviceID"].ToString());
}
return ls;
}
2、使用驱动信息(引入System.IO)
public static List<string> getDisk()
{
var drivers = DriveInfo.GetDrives();
List<string> ls = new List<string>();
foreach (var driver in drivers)
{
if (driver.DriveType != DriveType.Fixed)
{
continue;
}
ls.Add(driver.Name);
}
return ls;
}
3、使用环境信息
public static List<string> getDisk()
{
String[] drives = Environment.GetLogicalDrives();
return new List<string>(drives);
}
C# 获取本地电脑所有的盘符
public List<string> GetRemovableDeviceID()
{
List<string> deviceIDs = new List<string>();
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * From Win32_LogicalDisk ");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
switch (int.Parse(mo["DriveType"].ToString()))
{
case (int)DriveType.Removable: //可以移动磁盘
{
//MessageBox.Show("可以移动磁盘");
deviceIDs.Add(mo["DeviceID"].ToString());
break;
}
case (int)DriveType.Fixed: //本地磁盘
{
//MessageBox.Show("本地磁盘");
deviceIDs.Add(mo["DeviceID"].ToString());
break;
}
case (int)DriveType.CDRom: //CD rom drives
{
//MessageBox.Show("CD rom drives ");
break;
}
case (int)DriveType.Network: //网络驱动
{
//MessageBox.Show("网络驱动器 ");
break;
}
case (int)DriveType.Ram:
{
//MessageBox.Show("驱动器是一个 RAM 磁盘 ");
break;
}
case (int)DriveType.NoRootDirectory:
{
//MessageBox.Show("驱动器没有根目录 ");
break;
}
default: //defalut to folder
{
//MessageBox.Show("驱动器类型未知 ");
break;
}
}
}
return deviceIDs;
}
最后
以上就是畅快啤酒为你收集整理的C# .Net 获取系统盘符的全部内容,希望文章能够帮你解决C# .Net 获取系统盘符所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复