About network connection status programming

xiaoxiao2021-03-06  14

1. You can use WMI (Win2K & XP):

(First, you must create a project in VS.NET, then reference a .NET accessory: System.Management.dll, so your project can use WMI)

With windows 2000 & xp, undocumented:

Msndis_MediaconnectStatus - NDISMEDIACONNECTSTATUS:

(0 = connected ?, 1 = not connection?)

With windows xp:

Win32_NetworkAdapter - NetConnectionStatus:

Using system.management;

Static void reportConnection2000 ()

{

ManagementClass Mc = New ManagementClass (@ "root / wmi", @ "msndis_mediaconnectstatus", null;

ManagementObjectCollection moc = mc.getinstances ();

Foreach (ManagementObject Mo in MoC)

{

String name = (string) Mo ["instancename"];

Bool Active = (BOOL) MO ["Active"];

Uint status = (uint) Mo ["NDISMEDIACONNECTSTATUS"];

Console.writeline ("{0} / n / Tactive: {1} Media Status: {2}", name, activ, status;

}

}

Static void reportConnectionXP ()

{

ManagementClass MC = New ManagementClass (@ "win32_networkadapter);

ManagementObjectCollection moc = mc.getinstances ();

Foreach (ManagementObject Mo in MoC)

{

String name = (string) Mo ["name"];

Object Val = Mo ["NetConnectionStatus"];

IF (Val! = null)

Console.writeline ("{0} / n / tconnection status: {1}", name, (ushib) VAL);

Else

Console.writeLine ("{0} / n", name);

}

}

If it returns 0, the connection is indicated;

If it is returned 1, it means no connection;

The above is a local connection state;

============================================================================================================================================================================================================= =========

Use PING to get: process p = new process ();

P.StartInfo .WorkingDirectory = "C: //";

p.Startinfo.fileName = "ping.exe";

P.StartInfo.arguments = "192.168.52.31";

P.Startinfo.uShellexecute = false;

P.Startinfo.RedirectStandardOrdoutput = True;

P.StartInfo.createnowindow = true;

p.Start ();

String Output = p.standardoutput.readtoend ();

p.WaitForexit ();

Messagebox.show (output); // Get the output value of ping

转载请注明原文地址:https://www.9cbs.com/read-46280.html

New Post(0)