C#列出所有物理网络适配器的方法
程序员文章站
2022-06-14 17:26:32
本文实例讲述了c#列出所有物理网络适配器的方法。分享给大家供大家参考。具体如下:
using system;
using system.collections;...
本文实例讲述了c#列出所有物理网络适配器的方法。分享给大家供大家参考。具体如下:
using system; using system.collections; using system.collections.generic; using system.linq; using system.management; using system.text; namespace robvanderwoude { class listnics { public static arraylist nics = new arraylist( ); public static string computer = string.empty; // use global variables, so we only need to run the wmi queries once public static string nsrootwmi = computer + "root\\wmi"; public static string nsrootcimv2 = computer + "root\\cimv2"; public static managementobjectsearcher searcher1 = new managementobjectsearcher( nsrootwmi, "select * from msndis_physicalmediumtype" ); public static managementobjectcollection wmi1 = searcher1.get( ); public static managementobjectsearcher searcher2 = new managementobjectsearcher( nsrootcimv2, "select * from win32_networkadapter" ); public static managementobjectcollection wmi2 = searcher2.get( ); public static managementobjectsearcher searcher3 = new managementobjectsearcher( nsrootwmi, "select * from msndis_linkspeed" ); public static managementobjectcollection wmi3 = searcher3.get( ); static int main( string[] args ) { try { bool listbluetooth = true; bool listwired = true; bool listwireless = true; #region command line parsing // only 2 optional argument allowed: remote computer name and/or adapter type if ( args.length > 2 ) { return writeerror( "invalid command line arguments" ); } if ( args.length > 0 ) { foreach ( string arg in args ) { // we'll display a 'friendly' message if help was requested if ( arg.startswith( "/" ) || arg.startswith( "-" ) ) { switch ( arg.toupper( ) ) { case "/?": case "-?": return writeerror( string.empty ); case "/b": case "/bluetooth": if ( ( listbluetooth && listwired && listwireless ) == false ) { return writeerror( "select a single adapter type only, or omit type to select all" ); } listwired = false; listwireless = false; break; case "/w": case "/wired": if ( ( listbluetooth && listwired && listwireless ) == false ) { return writeerror( "select a single adapter type only, or omit type to select all" ); } listbluetooth = false; listwireless = false; break; case "/wl": case "/wifi": case "/wireless": if ( ( listbluetooth && listwired && listwireless ) == false ) { return writeerror( "select a single adapter type only, or omit type to select all" ); } listbluetooth = false; listwired = false; break; default: return writeerror( "invalid command line argument" ); } } else { if ( !string.isnullorempty( computer ) ) { return writeerror( "do not specify more than one computer name" ); } computer = "\\\\" + arg + "\\"; } } } #endregion command line parsing foreach ( managementobject queryobj1 in wmi1 ) { 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 ( string nic in nics ) { console.writeline( nic ); } return 0; } catch ( exception e ) { return writeerror( e ); } } public static void addadapter( string name, string type ) { foreach ( managementobject queryobj2 in wmi2 ) { if ( ( queryobj2["name"].tostring( ) == name ) && convert.toboolean( queryobj2["physicaladapter"] ) ) { foreach ( managementobject queryobj3 in wmi3 ) { 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 ); } } } } } #region error handling public static int writeerror( exception e ) { return writeerror( e == null ? null : e.message ); } public static int writeerror( string errormessage ) { 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, version 1.00" ); console.error.writeline( "list physical network adapters on the specified computer" ); 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\" is a remote computer name (default: this computer)" ); console.foregroundcolor = consolecolor.white; console.error.write( " /b" ); console.resetcolor( ); console.error.write( "luetooth or " ); console.foregroundcolor = consolecolor.white; console.error.write( "/b" ); console.resetcolor( ); console.error.writeline( " list bluetooth adapters only (default: all)" ); console.foregroundcolor = consolecolor.white; console.error.write( " /w" ); console.resetcolor( ); console.error.write( "ired or " ); console.foregroundcolor = consolecolor.white; console.error.write( "/w" ); console.resetcolor( ); console.error.writeline( " list wired adapters only (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( "ess or " ); console.foregroundcolor = consolecolor.white; console.error.write( "/wl" ); console.resetcolor( ); console.error.writeline( " list wireless adapters only (default: all)" ); console.error.writeline( ); console.error.writeline( "written by rob van der woude" ); return 1; } #endregion error handling } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: C#判断指定分区是否是ntfs格式的方法