C#PING操作を真似て、ホストが通信できるかどうかを監視する
2302 ワード
以下はC#コードで、直接コピーして使用できます.
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace PingMock
{
class MockerClass
{
private static Object thisobject = new Object();
public void Ping(string Host)
{
//string rtn = "";
ManagementObjectSearcher mos = null;
ManagementObjectCollection moc = null;
LogClass writeLog = new LogClass();
try
{
string searchString = "select * from win32_PingStatus where Address = '" + Host.Trim() + "'";
mos = new ManagementObjectSearcher(searchString);
moc = mos.Get();
foreach (ManagementObject mo in moc)
{
object obj = mo.Properties["StatusCode"].Value;
if (obj == null)
{
lock (thisobject)
{
writeLog.WriteLogFile(Host + " PING 。 。");
}
//return Host+" PING 。 。";
}
else
{
if (obj.ToString().Trim() == "0")
{
//rtn = "OK";
break;
}
lock (thisobject)
{
writeLog.WriteLogFile(Host + " PING !");
}
}
}
}
catch (Exception ex)
{
writeLog.WriteLogFile(Host + " PING , :" + ex.Message);
//rtn = Host+" PING , :"+ ex.Message;
}
finally
{
if (moc != null) moc.Dispose();
if (mos != null) mos.Dispose();
}
//return rtn;
}
}
}
城自身が必要とする形式を修正することができます.これは結果を返さず、ログに直接記録します.