C# 请教有方法可以读取硬盘大小么
没个驱动器容量相加得到的不一定是硬盘总容量. 展开
只能遍历盘符,计算总容量!
除非被隐藏,否则相加得到的就是硬盘总容量啊,1000/1024这个转换差的问题相信你肯定不会忽略!
1.项目添加对 System.Management 的引用
2.新建hardDiskPartition.cs 盘符信息类
///
/// 盘符信息
///
public class HardDiskPartition
{
#region Data
private string _PartitionName;
private double _FreeSpace;
private double _SumSpace;
#endregion //Data
#region Properties
///
/// 空余大小
///
public double FreeSpace
{
get { return _FreeSpace; }
set { this._FreeSpace = value; }
}
///
/// 使用空间
///
public double UseSpace
{
get { return _SumSpace - _FreeSpace; }
}
///
/// 总空间
///
public double SumSpace
{
get { return _SumSpace; }
set { this._SumSpace = value; }
}
///
/// 分区名称
///
public string PartitionName
{
get { return _PartitionName; }
set { this._PartitionName = value; }
}
///
/// 是否主分区
///
public bool IsPrimary
{
get
{
//判断是否为系统安装分区
if (System.Environment.GetEnvironmentVariable("windir").Remove(2) == this._PartitionName)
{
return true;
}
else
{
return false;
}
}
}
#endregion //Properties
}
3.获取盘符空间信息:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.IO;
namespace ExPortToExcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
try
{
List<HardDiskPartition> listInfo = GetDiskListInfo();
if (listInfo != null && listInfo.Count > 0)
{
listBox1.Items.Clear();
foreach(HardDiskPartition disk in listInfo)
{
listBox1.Items.Add(string.Format("{0} 总空间:{1} GB,剩余:{2} GB",
disk.PartitionName, ManagerDoubleValue(disk.SumSpace,1),
ManagerDoubleValue(disk.FreeSpace,1)));
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 处理Double值,精确到小数点后几位
/// </summary>
/// <param name="_value">值</param>
/// <param name="Length">精确到小数点后几位</param>
/// <returns>返回值</returns>
private double ManagerDoubleValue(double _value,int Length)
{
if (Length < 0)
{
Length = 0;
}
return System.Math.Round(_value, Length);
}
/// <summary>
/// 获取硬盘上所有的盘符空间信息列表
/// </summary>
/// <returns></returns>
private List<HardDiskPartition> GetDiskListInfo()
{
List<HardDiskPartition> list = null;
//指定分区的容量信息
try
{
SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);
ManagementObjectCollection diskcollection = searcher.Get();
if (diskcollection != null && diskcollection.Count > 0)
{
list = new List<HardDiskPartition>();
HardDiskPartition harddisk = null;
foreach (ManagementObject disk in searcher.Get())
{
int nType = Convert.ToInt32(disk["DriveType"]);
if (nType != Convert.ToInt32(DriveType.Fixed))
{
continue;
}
else
{
harddisk = new HardDiskPartition();
harddisk.FreeSpace = Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024 * 1024);
harddisk.SumSpace = Convert.ToDouble(disk["Size"]) / (1024 * 1024 * 1024);
harddisk.PartitionName = disk["DeviceID"].ToString();
list.Add(harddisk);
}
}
}
}
catch (Exception)
{
}
return list;
}
}
}
我尝试过,读取驱动器容量相加得到硬盘总大小,如果有一部分没有被分区呢.......那就没有盘符哦...
/// <summary>
/// 物理磁盘信息
/// </summary>
public static void GetPhysicalDiskInfo()
{
try
{
Console.WriteLine("物理磁盘信息");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_DiskDrive");
foreach (ManagementObject mo in searcher.Get())
{
// mo["MediaType"],类型,如“Fixed hard disk media”
// mo["SizeBytes "],容量,如“80023749120”(字节)
Console.WriteLine(mo["MediaType"]);
Console.WriteLine(mo["SizeBytes"]);
}
}
catch
{
}
}
打印出来就一句"Fixed hard disk media"
使用WMI获取这些系统信息是不太有保证的,例如你会发现,并不是每个win32类中的属性
http://www.lingzhong.cn/tech/22124.htm
经过测试,发现用系统API Create / DeviceIoControl 可以成功获取
http://www.vckbase.com/index.php/wv/507
可以封装为一个C++ DLL供C#调用,或者转成C#代码使用