C#获取磁盘空间大小 C#实现获取磁盘空间大小的方法
人气:0想了解C#实现获取磁盘空间大小的方法的相关内容吗,在本文为您仔细讲解C#获取磁盘空间大小的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,获取,磁盘空间,大小,方法,下面大家一起来学习吧。
本文实例讲述了C#实现获取磁盘空间大小的方法。分享给大家供大家参考。具体实现方法如下:
方法一:利用System.IO.DriveInfo.GetDrives方法来获取
复制代码 代码如下:
///
/// 获取指定驱动器的空间总大小(单位为B)
///
/// 只需输入代表驱动器的字母即可 (大写)
///
public static long GetHardDiskSpace(string str_HardDiskName)
{
long totalSize= new long();
str_HardDiskName=str_HardDiskName +":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == str_HardDiskName)
{
totalSize = drive.TotalSize / (1024 * 1024 * 1024);
}
}
return totalSize;
}
/// 获取指定驱动器的空间总大小(单位为B)
///
/// 只需输入代表驱动器的字母即可 (大写)
///
public static long GetHardDiskSpace(string str_HardDiskName)
{
long totalSize= new long();
str_HardDiskName=str_HardDiskName +":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == str_HardDiskName)
{
totalSize = drive.TotalSize / (1024 * 1024 * 1024);
}
}
return totalSize;
}
///
/// 获取指定驱动器的剩余空间总大小(单位为B)
///
/// 只需输入代表驱动器的字母即可
///
public static long GetHardDiskFreeSpace(string str_HardDiskName)
{
long freeSpace = new long();
str_HardDiskName = str_HardDiskName + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == str_HardDiskName)
{
freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024);
}
}
return freeSpace;
}
方法二:利用ManagementClass("Win32_LogicalDisk")来获取
复制代码 代码如下:
List<Dictionary<string, string>> diskInfoDic = new List<Dictionary<string, string>>();
ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection disks = diskClass.GetInstances();
foreach(ManagementObject disk in disks)
{
Dictionary<string, string> diskInfo = new Dictionary<string, string>();
try
{
// 磁盘名称
diskInfo["Name"] =disk["Name"].ToString();
// 磁盘描述
diskInfo["Description"]=disk["Description"].ToString();
// 磁盘总容量,可用空间,已用空间
if (System.Convert.ToInt64(disk["Size"]) > 0)
{
long totalSpace = System.Convert.ToInt64(disk["Size"]) / MB;
long freeSpace = System.Convert.ToInt64(disk["FreeSpace"]) / MB;
long usedSpace = totalSpace - freeSpace;
diskInfo["totalSpace"]=totalSpace.ToString();
diskInfo["usedSpace"]=usedSpace.ToString();
diskInfo["freeSpace"]=freeSpace.ToString();
}
diskInfoDic.Add(diskInfo);
}
catch(Exception ex)
{
Throw ex;
}
}
ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection disks = diskClass.GetInstances();
foreach(ManagementObject disk in disks)
{
Dictionary<string, string> diskInfo = new Dictionary<string, string>();
try
{
// 磁盘名称
diskInfo["Name"] =disk["Name"].ToString();
// 磁盘描述
diskInfo["Description"]=disk["Description"].ToString();
// 磁盘总容量,可用空间,已用空间
if (System.Convert.ToInt64(disk["Size"]) > 0)
{
long totalSpace = System.Convert.ToInt64(disk["Size"]) / MB;
long freeSpace = System.Convert.ToInt64(disk["FreeSpace"]) / MB;
long usedSpace = totalSpace - freeSpace;
diskInfo["totalSpace"]=totalSpace.ToString();
diskInfo["usedSpace"]=usedSpace.ToString();
diskInfo["freeSpace"]=freeSpace.ToString();
}
diskInfoDic.Add(diskInfo);
}
catch(Exception ex)
{
Throw ex;
}
}
希望本文所述对大家的C#程序设计有所帮助。
加载全部内容