1. 可以用WMI (Win2K & XP):
(首先要在VS.NET中创建一个项目,然后在添加引用中引用一个.net的装配件:System.Management.dll,这样你的项目才能使用WMI)
with Windows 2000 & XP, UNDOCUMENTED:
MSNdis_MediaConnectStatus - NdisMediaConnectStatus :
(0 = connected?, 1 = not connected?)
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, active, 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, (ushort) val );
else
Console.WriteLine( " {0}\n", name );
}
}
如果返回0则表示连接;
如果返回1则表示没连接;
上面得到的是本地的连接状态;
==========================================================
利用ping来得取:
Process p = new Process();
p.StartInfo .WorkingDirectory ="c:\\";
p.StartInfo.FileName = "ping.exe";
p.StartInfo.Arguments="192.168.52.31";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start ();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit ();
MessageBox.Show(output);//得到ping的输出值