一、使用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. Magento2与Magento1的区别有哪些

    magento2是15年正式上线的正式版,框架和写法跟magento1有很大区别,用到了命名空间和composer,模块化设计更强.因为是刚出生不久 所以bug比较多.目前全世界做magento2的公 ...

  2. hdu1240/poj2225 BFS广搜的再理解

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/guodongxiaren/article/details/37756347 原题地址 pid=124 ...

  3. 001-快速搭建Spring web应用【springboot 2.0.4】-gradle、springboot的启动过程分析、gradle多模块构建

    一.概述 学习<精通Spring MVC4>书籍笔记 二.笔记 1.快速构建Spring starter web项目几种方式 1>使用Spring Tool Suite生成Start ...

  4. 小程序编辑器vscode

    安装中文版 1)打开vscode工具: 2)使用快捷键组合[Ctrl+Shift+p],在搜索框中输入“configure display language”,点击确定后: 3)如图所示 =>安 ...

  5. python中的*arg和**kwargs

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

  6. Spark partitionBy

    partitionBy 重新分区, repartition默认采用HashPartitioner分区,自己设计合理的分区方法(比如数量比较大的key 加个随机数 随机分到更多的分区, 这样处理数据倾斜 ...

  7. Spring MVC 简介及入门小例子

    说明:文章内容全部截选自实验楼教程[Spring MVC 简易教程] 一.什么是 Spring MVC Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring ...

  8. MongoDB--搭建mongodb服务器

    此为手动搭建: 可以看到初始化data时所有的数据,和log里已经有日志文件

  9. Hybrid设计--Hybrid中Native能力的设计

    稍微成熟的团队,header一定是不利于业务的UI组件,这个组件会封装在view层,方便前端使用.对业务前端开发来说,不用关注header是如何实现的,只用框架层释放的API.(一个前端有一个自己的U ...

  10. fill和memset的区别

    https://blog.csdn.net/xs18952904/article/details/75195412 memset只能初始化成为0或者-1,其他都要用fill来完成. #include& ...