C#获取驱动器盘符
一、使用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#获取驱动器盘符的更多相关文章
- 批处理bat命令--获取当前盘符和当前目录和上级目录
批处理bat命令--获取当前盘符和当前目录和上级目录 批处理命令获取当前盘符和当前目录%~d0 是当前盘符%cd% 是当前目录可以用echo %cd%进行打印测试 以下例子是命令行编译Visual S ...
- Visual Subst - 简单将任意文件夹挂载模拟成驱动器盘符硬盘分区的小工具
随着电脑的使用,硬盘里的资料一天比一天多,也越来越杂乱.一些朋友为了方便文件管理,会考虑重新分区,让C.D.E等盘符分别担任不同的角色.不过,不分区的话也有一些小工具可以帮你实现. Visual Su ...
- C#检测获取移动硬盘盘符
最近做一个小工具, C# 对 移动硬盘的检测, var arr = DriveInfo.GetDrives(); 得出的所有磁盘,发现对于移动硬盘,DriveType 不是 Removable 类型 ...
- 批处理学习之Bat命令——获取当前盘符、当前目录、上级目录
命令 当前盘符:%~d0 当前路径:%cd% 当前执行命令行:%0 当前bat文件路径:%~dp0 当前bat文件短路径:%~sdp0 测试 下载testBatPath.bat测试文件,双击.bat运 ...
- C# 读取驱动器盘符及信息
System.IO.DriveInfo[] hardDiskDrives = System.IO.DriveInfo.GetDrives(); foreach (System.IO.DriveInfo ...
- NSIS:获取硬盘中容量最大的分区盘符
原文 NSIS:获取硬盘中容量最大的分区盘符 我们在安装一些在线视频软件比如迅雷看看时,会发现他们的安装程序会自动判断当前系统中容量最大的分区,以便在其中创建数据缓冲下载的文件夹,这种功能如果实现呢, ...
- 注册表与盘符(转victor888文章 )
转自: http://blog.csdn.net/loulou_ff/article/details/3769479 写点东西,把这阶段的研究内容记录下来,同时也给研究相关内容的同志提供参考, ...
- Delphi 自动检测U盘插入、拔出及获取U盘盘符!
http://qqhack8.blog.163.com/blog/static/1141479852012102133475/ Delphi 自动检测U盘插入.拔出及获取U盘盘符! u盘的 插 ...
- C#通过盘符获取剩余空间
public static long GetHardDiskSpace(string str_HardDiskName) { ; str_HardDiskName = str_HardDiskName ...
随机推荐
- [MySQL优化1]centos中MySQL列出所有表
步骤1 - 连接到MySQL数据库服务器:[root@host]# mysql -u root -pEnter password:******提示登陆成功Welcome to the MySQL mo ...
- 对Python源码加密及反编译前后对比
关于python的加密 目前软件开发商对 Python 加密时可能会有两种形式,一种是对python转成的exe进行 保护,另一种是直接对.py或者.pyc文件进行保护,下面将列举两种形式的保护流程. ...
- [py]环境变量的获取os.environ.get和设置
$ export name='maotai' $ python >>> import os >>> os.environ.get('name', "&qu ...
- tf运作方式之共享变量
转自:https://docs.pythontab.com/tensorflow/how_tos/variable_scope/ 这个讲的不错,能够更理解tf.get_Variable()了.创建时有 ...
- zhaoyin
1.什么时候用到事务,单个update操作会用到事务吗? 银行转账 /**//*--使用事务--*/ use stuDB go --恢复原来的数据 --update bank set currentM ...
- jenkins 实现多用户同时触发任务 + 修改job的config.xml 不重启服务生效
(后期添加: 这篇博客是在刚研究并发构建时写的,所以方法比较老套,采用的时流水线(pipeline)的方式,实现时通过如果job的用户配置来创建多个新的执行任务的job,并且将执行日志回收到入口job ...
- [LeetCode] 162. Find Peak Element_Medium tag: Binary Search
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- gparted 不能起作用的时候,用fdisk
我用的是vmware,从网上下载的centos 6.3版本,通过yum update,其最后更新为6系列的最终版本6.7. 没有仔细看磁盘空间,因为某种原因,为了远程登录,我安装了xfce4桌面,后来 ...
- cocos 简便斗地主发牌动画
niuniuSend : function (int) { switch(int) { case 0: for(var i=0;i<5;i++){ var sp = new ccui.Image ...
- cocos2d JS 鼠标响应事件
对于PC和超级本,添加鼠标事件的的处理,可以加强用户的体验,其处理逻辑与触摸事件基本一样,多了一些鼠标特有的事件响应 如滚轮事件(onMouseScroll) cc.eventManager.addL ...