Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listnics.cs

(view source code of listnics.cs as plain text)

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Text;
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class ListNICs
  11. 	{
  12. 		public static ArrayList nics = new ArrayList( );
  13. 		public static string computer = string.Empty;
  14.  
  15. 		// Use global variables, so we only need to run the WMI queries once
  16. 		public static string nsrootwmi = computer + "root\\WMI";
  17. 		public static string nsrootcimv2 = computer + "root\\CIMV2";
  18. 		public static ManagementObjectSearcher searcher1 = new ManagementObjectSearcher( nsrootwmi, "SELECT * FROM MSNdis_PhysicalMediumType" );
  19. 		public static ManagementObjectCollection wmi1 = searcher1.Get( );
  20. 		public static ManagementObjectSearcher searcher2 = new ManagementObjectSearcher( nsrootcimv2, "SELECT * FROM Win32_NetworkAdapter" );
  21. 		public static ManagementObjectCollection wmi2 = searcher2.Get( );
  22. 		public static ManagementObjectSearcher searcher3 = new ManagementObjectSearcher( nsrootwmi, "SELECT * FROM MSNdis_LinkSpeed" );
  23. 		public static ManagementObjectCollection wmi3 = searcher3.Get( );
  24.  
  25.  
  26. 		static int Main( string[] args )
  27. 		{
  28. 			try
  29. 			{
  30. 				bool listBluetooth = true;
  31. 				bool listWired = true;
  32. 				bool listWireless = true;
  33.  
  34.  
  35. 				#region Command line parsing
  36.  
  37.  
  38. 				// Only 2 optional argument allowed: remote computer name and/or adapter type
  39. 				if ( args.Length > 2 )
  40. 				{
  41. 					return WriteError( "Invalid command line arguments" );
  42. 				}
  43. 				if ( args.Length > 0 )
  44. 				{
  45. 					foreach ( string arg in args )
  46. 					{
  47. 						// We'll display a 'friendly' message if help was requested
  48. 						if ( arg.StartsWith( "/" ) || arg.StartsWith( "-" ) )
  49. 						{
  50. 							switch ( arg.ToUpper( ) )
  51. 							{
  52. 								case "/?":
  53. 								case "-?":
  54. 									return WriteError( string.Empty );
  55. 								case "/B":
  56. 								case "/BLUETOOTH":
  57. 									if ( ( listBluetooth && listWired && listWireless ) == false )
  58. 									{
  59. 										return WriteError( "Select a single adapter type only, or omit type to select all" );
  60. 									}
  61. 									listWired = false;
  62. 									listWireless = false;
  63. 									break;
  64. 								case "/W":
  65. 								case "/WIRED":
  66. 									if ( ( listBluetooth && listWired && listWireless ) == false )
  67. 									{
  68. 										return WriteError( "Select a single adapter type only, or omit type to select all" );
  69. 									}
  70. 									listBluetooth = false;
  71. 									listWireless = false;
  72. 									break;
  73. 								case "/WL":
  74. 								case "/WIFI":
  75. 								case "/WIRELESS":
  76. 									if ( ( listBluetooth && listWired && listWireless ) == false )
  77. 									{
  78. 										return WriteError( "Select a single adapter type only, or omit type to select all" );
  79. 									}
  80. 									listBluetooth = false;
  81. 									listWired = false;
  82. 									break;
  83. 								default:
  84. 									return WriteError( "Invalid command line argument" );
  85. 							}
  86. 						}
  87. 						else
  88. 						{
  89. 							if ( !string.IsNullOrEmpty( computer ) )
  90. 							{
  91. 								return WriteError( "Do not specify more than one computer name" );
  92. 							}
  93. 							computer = "\\\\" + arg + "\\";
  94. 						}
  95.  
  96. 					}
  97. 				}
  98.  
  99.  
  100. 				#endregion Command line parsing
  101.  
  102.  
  103. 				foreach ( ManagementObject queryObj1 in wmi1 )
  104. 				{
  105. 					if ( queryObj1["NdisPhysicalMediumType"].ToString( ) == "10" )
  106. 					{
  107. 						if ( listBluetooth )
  108. 						{
  109. 							AddAdapter( queryObj1["InstanceName"].ToString( ), "Bluetooth" );
  110. 						}
  111. 					}
  112. 					if ( queryObj1["NdisPhysicalMediumType"].ToString( ) == "0" )
  113. 					{
  114. 						if ( listWired )
  115. 						{
  116. 							AddAdapter( queryObj1["InstanceName"].ToString( ), "Wired" );
  117. 						}
  118. 					}
  119. 					if ( queryObj1["NdisPhysicalMediumType"].ToString( ) == "1" )
  120. 					{
  121. 						if ( listWireless )
  122. 						{
  123. 							AddAdapter( queryObj1["InstanceName"].ToString( ), "Wireless" );
  124. 						}
  125. 					}
  126. 				}
  127.  
  128. 				nics.Sort( );
  129.  
  130. 				foreach ( string nic in nics )
  131. 				{
  132. 					Console.WriteLine( nic );
  133. 				}
  134.  
  135.  
  136. 				return 0;
  137. 			}
  138. 			catch ( Exception e )
  139. 			{
  140. 				return WriteError( e );
  141. 			}
  142. 		}
  143.  
  144.  
  145. 		public static void AddAdapter( string name, string type )
  146. 		{
  147. 			foreach ( ManagementObject queryObj2 in wmi2 )
  148. 			{
  149. 				if ( ( queryObj2["Name"].ToString( ) == name ) && Convert.ToBoolean( queryObj2["PhysicalAdapter"] ) )
  150. 				{
  151. 					foreach ( ManagementObject queryObj3 in wmi3 )
  152. 					{
  153. 						if ( queryObj3["InstanceName"].ToString( ) == name )
  154. 						{
  155. 							nics.Add( String.Format( "{0,6}", Convert.ToInt32( queryObj3["NdisLinkSpeed"] ) / 10000 ) + " Mb/s\t" + String.Format( "{0,-11}", "[" + type + "]" ) + "\t" + name );
  156. 						}
  157. 					}
  158. 				}
  159. 			}
  160. 		}
  161.  
  162.  
  163. 		#region Error handling
  164.  
  165.  
  166. 		public static int WriteError( Exception e )
  167. 		{
  168. 			return WriteError( e == null ? null : e.Message );
  169. 		}
  170.  
  171.  
  172. 		public static int WriteError( string errorMessage )
  173. 		{
  174. 			/*
  175. 			ListNICs,  Version 1.00
  176. 			List physical network adapters on the specified computer
  177.  
  178. 			Usage:  LISTNICS  [ computername ]  [ /Bluetooth | /Wired | /WireLess ]
  179.  
  180. 			Where:  "computername"    is a remote computer name    (default: this computer)
  181. 			        /Bluetooth or /B  list Bluetooth adapters only (default: all)
  182. 			        /Wired     or /W  list wired adapters only     (default: all)
  183. 			        /Wireless  or /WL list wireless adapters only  (default: all)
  184.  
  185. 			Written by Rob van der Woude
  186. 			http://www.robvanderwoude.com
  187. 			*/
  188.  
  189. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  190. 			{
  191. 				Console.Error.WriteLine( );
  192. 				Console.ForegroundColor = ConsoleColor.Red;
  193. 				Console.Error.Write( "ERROR:  " );
  194. 				Console.ForegroundColor = ConsoleColor.White;
  195. 				Console.Error.WriteLine( errorMessage );
  196. 				Console.ResetColor( );
  197. 			}
  198. 			Console.Error.WriteLine( );
  199. 			Console.Error.WriteLine( "ListNICs,  Version 1.00" );
  200. 			Console.Error.WriteLine( "List physical network adapters on the specified computer" );
  201. 			Console.Error.WriteLine( );
  202. 			Console.Error.Write( "Usage:  " );
  203. 			Console.ForegroundColor = ConsoleColor.White;
  204. 			Console.Error.Write( "LISTNICS" );
  205. 			Console.ResetColor( );
  206. 			Console.Error.Write( "  [ computername ]  [ " );
  207. 			Console.ForegroundColor = ConsoleColor.White;
  208. 			Console.Error.Write( "/B" );
  209. 			Console.ResetColor( );
  210. 			Console.Error.Write( "luetooth | " );
  211. 			Console.ForegroundColor = ConsoleColor.White;
  212. 			Console.Error.Write( "/W" );
  213. 			Console.ResetColor( );
  214. 			Console.Error.Write( "ired | " );
  215. 			Console.ForegroundColor = ConsoleColor.White;
  216. 			Console.Error.Write( "/W" );
  217. 			Console.ResetColor( );
  218. 			Console.Error.Write( "ire" );
  219. 			Console.ForegroundColor = ConsoleColor.White;
  220. 			Console.Error.Write( "L" );
  221. 			Console.ResetColor( );
  222. 			Console.Error.WriteLine( "ess ]" );
  223. 			Console.Error.WriteLine( );
  224. 			Console.Error.WriteLine( "Where:  \"computername\"    is a remote computer name    (default: this computer)" );
  225. 			Console.ForegroundColor = ConsoleColor.White;
  226. 			Console.Error.Write( "        /B" );
  227. 			Console.ResetColor( );
  228. 			Console.Error.Write( "luetooth or " );
  229. 			Console.ForegroundColor = ConsoleColor.White;
  230. 			Console.Error.Write( "/B" );
  231. 			Console.ResetColor( );
  232. 			Console.Error.WriteLine( "  list Bluetooth adapters only (default: all)" );
  233. 			Console.ForegroundColor = ConsoleColor.White;
  234. 			Console.Error.Write( "        /W" );
  235. 			Console.ResetColor( );
  236. 			Console.Error.Write( "ired     or " );
  237. 			Console.ForegroundColor = ConsoleColor.White;
  238. 			Console.Error.Write( "/W" );
  239. 			Console.ResetColor( );
  240. 			Console.Error.WriteLine( "  list wired adapters only     (default: all)" );
  241. 			Console.ForegroundColor = ConsoleColor.White;
  242. 			Console.Error.Write( "        /W" );
  243. 			Console.ResetColor( );
  244. 			Console.Error.Write( "ire" );
  245. 			Console.ForegroundColor = ConsoleColor.White;
  246. 			Console.Error.Write( "L" );
  247. 			Console.ResetColor( );
  248. 			Console.Error.Write( "ess  or " );
  249. 			Console.ForegroundColor = ConsoleColor.White;
  250. 			Console.Error.Write( "/WL" );
  251. 			Console.ResetColor( );
  252. 			Console.Error.WriteLine( " list wireless adapters only  (default: all)" );
  253. 			Console.Error.WriteLine( );
  254. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  255. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  256. 			return 1;
  257. 		}
  258.  
  259.  
  260. 		#endregion Error handling
  261. 	}
  262. }
  263.  

page last modified: 2024-04-16; loaded in 0.0282 seconds