C#实现获取磁盘空间大小的方法
程序员文章站
2023-12-15 19:01:10
本文实例讲述了c#实现获取磁盘空间大小的方法。分享给大家供大家参考。具体实现方法如下:
方法一:利用system.io.driveinfo.getdrives方法来获取...
本文实例讲述了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#程序设计有所帮助。