一、使用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. Servlet----------ServletConfig

    1. 什么是 ServletConfig servletConfig对象:用于封装servlet的配置信息.从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对servlet自身有效 ...

  2. FFmpeg 裁剪——音频解码

    配置ffmpeg,只留下某些音频的配置: ./configure --enable-shared --disable-yasm --enable-memalign-hack --enable-gpl ...

  3. 【Selenium】selenium中隐藏元素如何定位?

    前言 面试题:selenium 中隐藏元素如何定位?这个是很多面试官喜欢问的一个题,如果单纯的定位的话,隐藏元素和普通不隐藏元素定位没啥区别,用正常定位方法就行了 但是吧~~~很多面试官自己都搞不清楚 ...

  4. MySQL innodb_flush_method 【转载】

    innodb_flush_method这个参数控制着innodb数据文件及redo log的打开.刷写模式,对于这个参数,文档上是这样描述的: 有三个值:fdatasync(默认),O_DSYNC,O ...

  5. (1.7)mysql profiles分析

    mysql profiles分析 作用:记录会话查询SQL所用时间 1.开启 2.使用 [2.1]先使用一个查询 [2.2]然后再运行 show profiles; [2.3]查看执行过程中每个状态和 ...

  6. Laravel上传产品图片Uploading img

    这节我们讲Laravel产品图片上传,有很多方式可以实现,这里我们用intervention/image插件来进行.首先安装intervention/image插件,在命令行输入 composer r ...

  7. 006-docker-安装-nginx

    1.搜索镜像 docker search nginx 2.拉取合适镜像 docker pull nginx docker images 3.使用镜像 docker run -p 8080:80 --n ...

  8. wxPython:文本对话框TextEntryDialog

    wxTextEntryDialog(wxWindow* parent, const wxString& message, const wxString& caption = " ...

  9. vue-filter

  10. Spark DataFrame列的合并与拆分

    版本说明:Spark-2.3.0 使用Spark SQL在对数据进行处理的过程中,可能会遇到对一列数据拆分为多列,或者把多列数据合并为一列.这里记录一下目前想到的对DataFrame列数据进行合并和拆 ...