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 ...
随机推荐
- Linux ifconfig 命令
在centos6 自带ifconfig 在centos7 默认不带ifconfig,需要自己安装 ifconfig命令用来配置或查看网卡接口,常见用法如下: 安装ifconfig命令 [root@my ...
- mysql \c 终止 mysql输入语句模式
\c 遇到这种情况怎么退出mysql 输入语句模式? mysql> select -> 输入\c退出 mysql> select -> \c mysql> 另外一种情况 ...
- pycharm 如何设置方法调用字体颜色
一.pycharm 如何设置方法调用字体颜色 1.打开pycharm编辑器,file > settings > editor > color scheme > python & ...
- idea-常用插件-nginx
1.mac上nginx安装 brew search nginx brew install nginx 当然也可以编译安装 安装完以后,可以在终端输出的信息里看到一些配置路径: /usr/local/e ...
- Oracle(2)之多表查询&子查询&集合运算
多表查询 笛卡尔积 同时查询多张表时,每张表的每条数据都要和其它表的每条数据做组合.如下栗子,我们发现产生的总记录数是 56 条,还发现 emp 表是 14 条,dept 表是 4 条,56 条正是 ...
- PinyinUtil
import java.util.HashSet;import java.util.Set;import java.util.regex.Matcher;import java.util.regex. ...
- oracle新建表空间与用户
例如:创建用户 cwbase1_9999 密码 gxtest 表空间 cwdata1 sqlplus / as sysdba 运行sql create tablespace cwdata1 logg ...
- 纯真IP数据库格式详解 附demo
纯真版IP数据库,优点是记录多,查询速度快,它只用一个文件QQWry.dat就包含了所有记录,方便嵌入到其他程序中,也方便升级.缺点是你想要编辑它却是比较麻烦的,由于其文件格式的限制,你要直接添加IP ...
- [LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...
- [LeetCode] 840. Magic Squares In Grid_Easy
A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, co ...