using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CHENT_MODEL
{
/// <summary>
/// 硬盘信息类
/// </summary>
public class DiskInfo
{
public DiskInfo(string strDiskName, string strVolumeName, string lDiskSize, stringlDiskFreeSpace, string strDiskFileSystem)
{
this.strDiskName = strDiskName;//磁盘名称
this.strVolumeName = strVolumeName;//磁盘标卷
this.lDiskSize = lDiskSize;//磁盘大小
this.lDiskFreeSpace = lDiskFreeSpace;//可用磁盘空间
this.strDiskFileSystem = strDiskFileSystem;//文件系统(FAT32,NTFS等)
}
private string m_strDiskName;
/// <summary>
/// 磁盘名称
/// </summary>
public string strDiskName
{
get { return m_strDiskName; }
set { m_strDiskName = value; }
}
private string m_strVolumeName;
/// <summary>
/// 磁盘标卷
/// </summary>
public string strVolumeName
{
get { return m_strVolumeName; }
set { m_strVolumeName = value; }
}
private string m_lDiskSize;
/// <summary>
/// 磁盘大小
/// </summary>
public string lDiskSize
{
get { return m_lDiskSize; }
set { m_lDiskSize = value; }
}
private string m_lDiskFreeSpace;
/// <summary>
/// 可用磁盘空间
/// </summary>
public string lDiskFreeSpace
{
get { return m_lDiskFreeSpace; }
set { m_lDiskFreeSpace = value; }
}
private string m_strDiskFileSystem;
/// <summary>
/// 文件系统(FAT32,NTFS等)
/// </summary>
public string strDiskFileSystem
{
get { return m_strDiskFileSystem; }
set { m_strDiskFileSystem = value; }
}
}
}
方法类
#region 获得分区信息
/// <summary>
/// 获取分区信息
/// </summary>
private List<DiskInfo> GetDiskInfo()
{
List<DiskInfo> lDiskInfoStruct = new List<DiskInfo>();
try
{
//实例化一个ManagementClass类,并将Win32_LogicalDisk作为参数传递进去,
//这样就可以查询Win32_LogicalDisk这个类里面的一些信息了。
ManagementClass mClass = new ManagementClass("Win32_LogicalDisk");
//获取Win32_LogicalDisk这个类的所有实例
ManagementObjectCollection moCollection = mClass.GetInstances();
//对Win32_OperatingSystem这个类进行遍历
foreach (ManagementObject mObject in moCollection)
{
//判断磁盘是否为固定磁盘,即硬盘
if (Int32.Parse(mObject["DriveType"].ToString()) == (int)System.IO.DriveType.Fixed)
{
lDiskInfoStruct.Add(new DiskInfo(mObject["Name"].ToString(),mObject["VolumeName"].ToString(),
(long.Parse(mObject["Size"].ToString()) / 1024.0 / 1024.0 / 1024.0).ToString("0.000") + "G",
(long.Parse(mObject["FreeSpace"].ToString()) / 1024.0 / 1024.0 / 1024.0).ToString("0.000") + "G",
mObject["FileSystem"].ToString()));
//diskInfoStruct.strDiskName = mObject["Name"].ToString(); //获取磁盘名称
//diskInfoStruct.strVolumeName = mObject["VolumeName"].ToString(); //获取磁盘标卷
//diskInfoStruct.lDiskSize = (long.Parse(mObject["Size"].ToString())/1024.0/1024.0).ToString (); //获取磁盘大小
//diskInfoStruct.lDiskFreeSpace = (long.Parse(mObject["FreeSpace"].ToString())/1024.0/1024.0).ToString (); //获取磁盘可用空间
//diskInfoStruct.strDiskFileSystem = mObject["FileSystem"].ToString(); //获取磁盘文件系统
//lDiskInfo.Add(diskInfoStruct);
}
}
}
catch
{ }
return lDiskInfoStruct;
}
/// <summary>
/// 格式化磁盘信息
/// </summary>
public string Disk_String()
{
string Disk="";
List<DiskInfo> lDiskInfoStruct = GetDiskInfo();
foreach (DiskInfo str in lDiskInfoStruct)
{
Disk = Disk + str.strDiskName + "%%%%";
Disk = Disk + str.strVolumeName + "%%%%";
Disk = Disk + str.lDiskSize + "%%%%";
Disk = Disk + str.lDiskFreeSpace + "%%%%";
Disk = Disk + str.strDiskFileSystem + "%%%%";
}
return Disk;
}