110,538
社区成员
发帖
与我相关
我的任务
分享
public struct OVERLAPPED
{
public IntPtr Internal;
public IntPtr InternalHigh;
public IntPtr Pointer;
public IntPtr hEvent;
};
[DllImport("Iphlpapi.dll")]
public extern static uint NotifyAddrChange(ref IntPtr handle, ref OVERLAPPED overlapped);
[DllImport("Ws2_32.dll")]
public extern static IntPtr WSACreateEvent();
[DllImport("Ws2_32.dll")]
public extern static int WSAGetLastError();
[DllImport("Kernel32.dll")]
public static extern int WaitForSingleObject(IntPtr hHandle, int dwMilliseconds);
void GetAddrChangeEvent()
{
OVERLAPPED overlapped = new OVERLAPPED();
IntPtr hand = IntPtr.Zero;
overlapped.hEvent = WSACreateEvent();
uint ret = NotifyAddrChange(ref hand, ref overlapped);
if (0 != ret)
{
if (997L != WSAGetLastError())
{
// some error!
return;
}
}
if (WaitForSingleObject(overlapped.hEvent, (int)-1) == 0)
{
// addr change
// MessageBox.Show("some changed!");
}
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(GetAddrChangeEvent);
thread.Start();
}
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
// 调用的是 CMD.EXE
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
Console.Write("Try to ping " + Paras[1] + "("+Paras[Paras.Count()-1] +")"+ "\r\n");
p.StandardInput.WriteLine("ping -n 1 " + Paras[1]);
// ping –n 1 IP
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.Close();
//取得输出结果
string result;
if (strRst.IndexOf("(0% loss)") != -1 || (strRst.IndexOf("(0% 丢失)") != -1 && (strRst.IndexOf("无法访问目标主机") == -1)))
result = "网络正常";
else
{
result = "网络异常,请检查网络情况!";
}
return result;