VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C#获取本地磁盘信息

直接上干货简单易懂

//磁盘监控(远程/本地)
//需要引用System.Management.dll
public class RemoteMonitoring
{
private static string strMsg = "";
private static long freesize = 0, size = 0;
private static long gb = 1024 * 1024 * 1024;//GB,这里可以自己定义获取是MB还是GB

public static string ThreadDisk()
{
strMsg = "";
string strHostName = Dns.GetHostName(); //取得本机的主机名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP
string localip = ipEntry.AddressList[0].ToString(); //假设本地主机为单网卡,取得第一个IP
try
{
long minsize = long.Parse(CommonField.minsize);
if (CommonField.ip == localip)
{
#region 本机
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Fixed)
{
freesize = drive.TotalFreeSpace / gb;
size = drive.TotalSize / gb;
if (freesize <= minsize)
{
strMsg += localip + "磁盘" + drive.Name + "的可用空间为" + freesize + "GB,请及时清理!" + System.Environment.NewLine;
}
strMsg += "磁盘" + drive.Name + "的总空间:" + size + "MB!" + "可用空间:" + freesize + "GB!" + System.Environment.NewLine;
}
}
#endregion
}
return strMsg;
}
catch (Exception ex)
{
strMsg = ex.Message;
}
return strMsg;
}

}


相关教程