C#列出所有物理网络适配器的方法
本文实例讲述了C#列出所有物理网络适配器的方法。分享给大家供大家参考。具体如下:
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Management;
usingSystem.Text;
namespaceRobvanderWoude
{
classListNICs
{
publicstaticArrayListnics=newArrayList();
publicstaticstringcomputer=string.Empty;
//Useglobalvariables,soweonlyneedtoruntheWMIqueriesonce
publicstaticstringnsrootwmi=computer+"root\\WMI";
publicstaticstringnsrootcimv2=computer+"root\\CIMV2";
publicstaticManagementObjectSearchersearcher1=newManagementObjectSearcher(nsrootwmi,"SELECT*FROMMSNdis_PhysicalMediumType");
publicstaticManagementObjectCollectionwmi1=searcher1.Get();
publicstaticManagementObjectSearchersearcher2=newManagementObjectSearcher(nsrootcimv2,"SELECT*FROMWin32_NetworkAdapter");
publicstaticManagementObjectCollectionwmi2=searcher2.Get();
publicstaticManagementObjectSearchersearcher3=newManagementObjectSearcher(nsrootwmi,"SELECT*FROMMSNdis_LinkSpeed");
publicstaticManagementObjectCollectionwmi3=searcher3.Get();
staticintMain(string[]args)
{
try
{
boollistBluetooth=true;
boollistWired=true;
boollistWireless=true;
#regionCommandlineparsing
//Only2optionalargumentallowed:remotecomputernameand/oradaptertype
if(args.Length>2)
{
returnWriteError("Invalidcommandlinearguments");
}
if(args.Length>0)
{
foreach(stringarginargs)
{
//We'lldisplaya'friendly'messageifhelpwasrequested
if(arg.StartsWith("/")||arg.StartsWith("-"))
{
switch(arg.ToUpper())
{
case"/?":
case"-?":
returnWriteError(string.Empty);
case"/B":
case"/BLUETOOTH":
if((listBluetooth&&listWired&&listWireless)==false)
{
returnWriteError("Selectasingleadaptertypeonly,oromittypetoselectall");
}
listWired=false;
listWireless=false;
break;
case"/W":
case"/WIRED":
if((listBluetooth&&listWired&&listWireless)==false)
{
returnWriteError("Selectasingleadaptertypeonly,oromittypetoselectall");
}
listBluetooth=false;
listWireless=false;
break;
case"/WL":
case"/WIFI":
case"/WIRELESS":
if((listBluetooth&&listWired&&listWireless)==false)
{
returnWriteError("Selectasingleadaptertypeonly,oromittypetoselectall");
}
listBluetooth=false;
listWired=false;
break;
default:
returnWriteError("Invalidcommandlineargument");
}
}
else
{
if(!string.IsNullOrEmpty(computer))
{
returnWriteError("Donotspecifymorethanonecomputername");
}
computer="\\\\"+arg+"\\";
}
}
}
#endregionCommandlineparsing
foreach(ManagementObjectqueryObj1inwmi1)
{
if(queryObj1["NdisPhysicalMediumType"].ToString()=="10")
{
if(listBluetooth)
{
AddAdapter(queryObj1["InstanceName"].ToString(),"Bluetooth");
}
}
if(queryObj1["NdisPhysicalMediumType"].ToString()=="0")
{
if(listWired)
{
AddAdapter(queryObj1["InstanceName"].ToString(),"Wired");
}
}
if(queryObj1["NdisPhysicalMediumType"].ToString()=="1")
{
if(listWireless)
{
AddAdapter(queryObj1["InstanceName"].ToString(),"Wireless");
}
}
}
nics.Sort();
foreach(stringnicinnics)
{
Console.WriteLine(nic);
}
return0;
}
catch(Exceptione)
{
returnWriteError(e);
}
}
publicstaticvoidAddAdapter(stringname,stringtype)
{
foreach(ManagementObjectqueryObj2inwmi2)
{
if((queryObj2["Name"].ToString()==name)&&Convert.ToBoolean(queryObj2["PhysicalAdapter"]))
{
foreach(ManagementObjectqueryObj3inwmi3)
{
if(queryObj3["InstanceName"].ToString()==name)
{
nics.Add(String.Format("{0,6}",Convert.ToInt32(queryObj3["NdisLinkSpeed"])/10000)+"Mb/s\t"+String.Format("{0,-11}","["+type+"]")+"\t"+name);
}
}
}
}
}
#regionErrorhandling
publicstaticintWriteError(Exceptione)
{
returnWriteError(e==null?null:e.Message);
}
publicstaticintWriteError(stringerrorMessage)
{
if(string.IsNullOrEmpty(errorMessage)==false)
{
Console.Error.WriteLine();
Console.ForegroundColor=ConsoleColor.Red;
Console.Error.Write("ERROR:");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.WriteLine(errorMessage);
Console.ResetColor();
}
Console.Error.WriteLine();
Console.Error.WriteLine("ListNICs,Version1.00");
Console.Error.WriteLine("Listphysicalnetworkadaptersonthespecifiedcomputer");
Console.Error.WriteLine();
Console.Error.Write("Usage:");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("LISTNICS");
Console.ResetColor();
Console.Error.Write("[computername][");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("/B");
Console.ResetColor();
Console.Error.Write("luetooth|");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("/W");
Console.ResetColor();
Console.Error.Write("ired|");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("/W");
Console.ResetColor();
Console.Error.Write("ire");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("L");
Console.ResetColor();
Console.Error.WriteLine("ess]");
Console.Error.WriteLine();
Console.Error.WriteLine("Where:\"computername\"isaremotecomputername(default:thiscomputer)");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("/B");
Console.ResetColor();
Console.Error.Write("luetoothor");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("/B");
Console.ResetColor();
Console.Error.WriteLine("listBluetoothadaptersonly(default:all)");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("/W");
Console.ResetColor();
Console.Error.Write("iredor");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("/W");
Console.ResetColor();
Console.Error.WriteLine("listwiredadaptersonly(default:all)");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("/W");
Console.ResetColor();
Console.Error.Write("ire");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("L");
Console.ResetColor();
Console.Error.Write("essor");
Console.ForegroundColor=ConsoleColor.White;
Console.Error.Write("/WL");
Console.ResetColor();
Console.Error.WriteLine("listwirelessadaptersonly(default:all)");
Console.Error.WriteLine();
Console.Error.WriteLine("WrittenbyRobvanderWoude");
return1;
}
#endregionErrorhandling
}
}
希望本文所述对大家的C#程序设计有所帮助。