直接上乾貨簡單易懂 //磁碟監控(遠程/本地) //需要引用System.Management.dll public class RemoteMonitoring { private static string strMsg = ""; private static long freesize = ...
直接上乾貨簡單易懂
//磁碟監控(遠程/本地)
//需要引用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;
}
}