一、使用ManagementObjectSearcher类

        static void Main(string[] args)
{
SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);
foreach (ManagementObject disk in searcher.Get())
{
//获取驱动器盘符
Console.WriteLine(disk["Name"].ToString());
//卷标
Console.WriteLine(disk["VolumeName"].ToString());
//驱动器类型
string DriveType = disk["DriveType"].ToString();
switch (DriveType)
{
case "":
Console.WriteLine("未知设备");
break;
case "":
Console.WriteLine("未分区");
break;
case "":
Console.WriteLine("可移动磁盘");
break;
case "":
Console.WriteLine("硬盘");
break;
case "":
Console.WriteLine("网络驱动器");
break;
case "":
Console.WriteLine("光驱");
break;
case "":
Console.WriteLine("内存磁盘");
break;
}
//容量
Console.WriteLine(GetSizeUseUnit(disk["Size"].ToString()));
//剩余空间
Console.WriteLine(GetSizeUseUnit(disk["FreeSpace"].ToString()));
}
} public static string GetSizeUseUnit(string size)
{
double dSpace = Convert.ToDouble(size);
string sSpace = dSpace.ToString("N");
string[] tmp;
string rtnSize = "";
tmp = sSpace.Split(',');
switch (tmp.GetUpperBound())
{
case :
rtnSize = tmp[] + " 字节";
break;
case :
rtnSize = tmp[] + "." + tmp[].Substring(, ) + " K";
break;
case :
rtnSize = tmp[] + "." + tmp[].Substring(, ) + " M";
break;
case :
rtnSize = tmp[] + "." + tmp[].Substring(, ) + " G";
break;
case :
rtnSize = tmp[] + "." + tmp[].Substring(, ) + " T";
break;
}
return rtnSize;
}

二、使用DriveInfo类

        static void Main(string[] args)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace); Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace); Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
}

三、使用Environment类,调用kernel32API获取驱动类型

        [DllImport("kernel32")]
public static extern uint GetDriveType(string lpRootPathName); static void Main(string[] args)
{
string[] drives = Environment.GetLogicalDrives();
foreach (string drive in drives)
{
//Determine icon to display by drive
switch (GetDriveType(drive))
{
case :
Console.WriteLine("软盘");
break;
case :
Console.WriteLine("硬盘");
break;
case :
Console.WriteLine("网络驱动器");
break;
case :
Console.WriteLine("光驱驱动器");
break;
default:
Console.WriteLine("");
break;
}
}
}

C#获取驱动器盘符的更多相关文章

  1. 批处理bat命令--获取当前盘符和当前目录和上级目录

    批处理bat命令--获取当前盘符和当前目录和上级目录 批处理命令获取当前盘符和当前目录%~d0 是当前盘符%cd% 是当前目录可以用echo %cd%进行打印测试 以下例子是命令行编译Visual S ...

  2. Visual Subst - 简单将任意文件夹挂载模拟成驱动器盘符硬盘分区的小工具

    随着电脑的使用,硬盘里的资料一天比一天多,也越来越杂乱.一些朋友为了方便文件管理,会考虑重新分区,让C.D.E等盘符分别担任不同的角色.不过,不分区的话也有一些小工具可以帮你实现. Visual Su ...

  3. C#检测获取移动硬盘盘符

    最近做一个小工具,  C# 对 移动硬盘的检测, var arr = DriveInfo.GetDrives(); 得出的所有磁盘,发现对于移动硬盘,DriveType 不是 Removable 类型 ...

  4. 批处理学习之Bat命令——获取当前盘符、当前目录、上级目录

    命令 当前盘符:%~d0 当前路径:%cd% 当前执行命令行:%0 当前bat文件路径:%~dp0 当前bat文件短路径:%~sdp0 测试 下载testBatPath.bat测试文件,双击.bat运 ...

  5. C# 读取驱动器盘符及信息

    System.IO.DriveInfo[] hardDiskDrives = System.IO.DriveInfo.GetDrives(); foreach (System.IO.DriveInfo ...

  6. NSIS:获取硬盘中容量最大的分区盘符

    原文 NSIS:获取硬盘中容量最大的分区盘符 我们在安装一些在线视频软件比如迅雷看看时,会发现他们的安装程序会自动判断当前系统中容量最大的分区,以便在其中创建数据缓冲下载的文件夹,这种功能如果实现呢, ...

  7. 注册表与盘符(转victor888文章 )

    转自: http://blog.csdn.net/loulou_ff/article/details/3769479     写点东西,把这阶段的研究内容记录下来,同时也给研究相关内容的同志提供参考, ...

  8. Delphi 自动检测U盘插入、拔出及获取U盘盘符!

    http://qqhack8.blog.163.com/blog/static/1141479852012102133475/     Delphi 自动检测U盘插入.拔出及获取U盘盘符! u盘的 插 ...

  9. C#通过盘符获取剩余空间

    public static long GetHardDiskSpace(string str_HardDiskName) { ; str_HardDiskName = str_HardDiskName ...

随机推荐

  1. RSA 时序攻击

    RSA的破解从理论上来讲是大数质数分解,可是就是有一些人另辟蹊径,根据你解密的时间长短就能破解你的RSA私钥. 举一个不恰当但是比较容易理解的例子: 密文0101 私钥0110 明文0100 问题的关 ...

  2. yaml语言在线可视化翻译

    yaml语言在线可视化翻译 https://editor.swagger.io/

  3. rsyncd的配置和使用

    服务器端配置文件说明 # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for mo ...

  4. Windows 7中200M神秘隐藏分区

    裸机全新安装Windows 7的用户,在安装完成后运行diskmgmt.msc打开磁盘管理器,可以看到在系统分区(一般为C分区)之前有一个大小为200MB的隐藏分区.这个特殊的隐藏分区与Windows ...

  5. python中的*arg和**kwargs

    arg对应多出来的位置参数,把它们解析成tuple;kwargs把关键字参数解析成dict. def example(pram): print(pram) def example2(param, *a ...

  6. 循环匹配出图片地址(即src属性)

    <script type="text/javascript"> //思路分两步:作者(yanue). //1,匹配出图片img标签(即匹配出所有图片),过滤其他不需要的 ...

  7. 将中文字符串分割为数组 解决str_split中文乱码php

    首先来介绍str_split()这个函数: 它的作用是将字符串分割为数组: 例如: $str='abcde';str_plite($str); 打印结果如下:Array(    [0] => a ...

  8. Python 全栈开发三 python基础 条件与循环

    一. 条件语句 python条件语句是根据一条或多条语句的执行结果的真假(True Or False)来决定代码块的执行. 而执行内容可以多行,以缩进来区分表示同一范围. 1.Python判断条件真假 ...

  9. inner join, left join, right join 和 full join

    inner join:理解为“有效连接”,两张表中都有的数据才会显示left join:理解为“有左显示”,比如on a.field=b.field,则显示a表中存在的全部数据及a.b中都有的数据,a ...

  10. mac xcode 常见配置

    1.报错:There are no schemes in workspace "..." 设置scheme共享,方法: 2.Build 文件夹是中间文件的保存地方,如何设置在工程目 ...