Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for systeminformation.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class SystemInformationWrapper
  11. 	{
  12. 		static string progver = "1.03";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			#region Initialize Variables
  18.  
  19. 			int rc = 0;
  20. 			int maxnamelength = 0;
  21. 			SortedList<string, string> requestedproperties = new SortedList<string, string>( );
  22. 			SortedList<string, string> propertynames = new SortedList<string, string>( );
  23. 			SortedList<string, int> propertyindices = new SortedList<string, int>( );
  24. 			bool ignoreinvalidproperties = false;
  25. 			bool listall = true;
  26. 			bool specificproperties = false;
  27. 			bool openurl = false;
  28. 			string url = "https://msdn.microsoft.com/library/system.windows.forms.systeminformation.aspx";
  29.  
  30. 			#endregion Initialize Variables
  31.  
  32.  
  33. 			#region Parse Command Line
  34.  
  35. 			if ( args.Length > 0 )
  36. 			{
  37. 				foreach ( string arg in args )
  38. 				{
  39. 					if ( arg == "/?" )
  40. 					{
  41. 						return ShowHelp( );
  42. 					}
  43. 					else if ( arg.ToUpper( ) == "/I" )
  44. 					{
  45. 						if ( ignoreinvalidproperties )
  46. 						{
  47. 							return ShowHelp( "Duplicate command line switch /I" );
  48. 						}
  49. 						ignoreinvalidproperties = true;
  50. 					}
  51. 					else if ( arg.ToUpper( ) == "/L" )
  52. 					{
  53. 						listall = true;
  54. 					}
  55. 					else if ( arg.ToUpper( ) == "/U" )
  56. 					{
  57. 						if ( openurl )
  58. 						{
  59. 							return ShowHelp( "Duplicate command line switch /U" );
  60. 						}
  61. 						openurl = true;
  62. 					}
  63. 					else if ( arg[0] == '/' )
  64. 					{
  65. 						return ShowHelp( "Invalid command line switch {0}", arg.ToUpper( ) );
  66. 					}
  67. 					else
  68. 					{
  69. 						requestedproperties.Add( arg.ToLower( ), arg );
  70. 						specificproperties = true;
  71. 						listall = false;
  72. 					}
  73. 				}
  74. 			}
  75.  
  76.  
  77. 			if ( listall && specificproperties )
  78. 			{
  79. 				return ShowHelp( "Either specificy one or more properties, or /L to list all, but not both" );
  80. 			}
  81.  
  82. 			#endregion Parse Command Line
  83.  
  84.  
  85. 			#region Make a List of Available Properties
  86.  
  87. 			int index = 0;
  88. 			foreach ( _PropertyInfo sp in typeof( SystemInformation ).GetProperties( ) )
  89. 			{
  90. 				propertynames.Add( sp.Name.ToLower( ), sp.Name );
  91. 				propertyindices.Add( sp.Name, index );
  92. 				if ( sp.Name.Length > maxnamelength )
  93. 				{
  94. 					maxnamelength = sp.Name.Length;
  95. 				}
  96. 				if ( !listall )
  97. 				{
  98. 					if ( requestedproperties.Keys.Contains( sp.Name.ToLower( ) ) )
  99. 					{
  100. 						requestedproperties[sp.Name.ToLower( )] = sp.Name;
  101. 					}
  102. 					// Translate requested indices to property names
  103. 					if ( requestedproperties.Keys.Contains( index.ToString( ) ) )
  104. 					{
  105. 						requestedproperties[sp.Name.ToLower( )] = sp.Name;
  106. 						requestedproperties.Remove( index.ToString( ) );
  107. 					}
  108. 				}
  109. 				index += 1;
  110. 			}
  111.  
  112. 			#endregion Make a List of Available Properties
  113.  
  114.  
  115. 			if ( listall )
  116. 			{
  117. 				// Default: list all properties and their values
  118. 				// Show table head
  119. 				Console.WriteLine( "{0,-" + maxnamelength + "}    Index:      Value:", "Property Name:" );
  120. 				Console.WriteLine( "{0,-" + maxnamelength + "}    ======      ======", "==============" );
  121. 				foreach ( string property in propertynames.Values )
  122. 				{
  123. 					object propval = typeof( SystemInformation ).GetProperty( property ).GetValue( typeof( SystemInformation ), null );
  124. 					string propertyvalue = propval.ToString( );
  125. 					// PowerStatus has to be handled separately, by default it only returns a string "System.Windows.Forms.PowerStatus"
  126. 					if ( property == "PowerStatus" )
  127. 					{
  128. 						propertyvalue = GetPowerStatus( );
  129. 					}
  130. 					Console.WriteLine( "{0,-" + maxnamelength + "}    {1,6}      {2}", property, propertyindices[property], propertyvalue );
  131. 				}
  132. 			}
  133. 			else
  134. 			{
  135. 				foreach ( string requestedproperty in requestedproperties.Keys )
  136. 				{
  137. 					if ( propertynames.Keys.Contains( requestedproperty ) )
  138. 					{
  139. 						// Get the selected property's value
  140. 						string propertyvalue = Convert.ToString( typeof( SystemInformation ).GetProperty( requestedproperties[requestedproperty] ).GetValue( typeof( SystemInformation ), null ) );
  141. 						// PowerStatus has to be handled separately, by default it only returns a string "System.Windows.Forms.PowerStatus"
  142. 						if ( requestedproperties[requestedproperty] == "PowerStatus" )
  143. 						{
  144. 							propertyvalue = GetPowerStatus( );
  145. 						}
  146. 						// Try if the return value can be set to the selected property's value
  147. 						try
  148. 						{
  149. 							rc = Convert.ToInt32( propertyvalue );
  150. 						}
  151. 						catch ( Exception )
  152. 						{
  153. 							rc = 0;
  154. 						}
  155. 						// Display selected property and its value
  156. 						Console.WriteLine( "{0}={1}", requestedproperties[requestedproperty], propertyvalue );
  157. 					}
  158. 					else if ( !ignoreinvalidproperties )
  159. 					{
  160. 						return ShowHelp( "Invalid property \"{0}\"", requestedproperty );
  161. 					}
  162. 				}
  163.  
  164.  
  165. 				if ( requestedproperties.Count > 1 )
  166. 				{
  167. 					rc = 0;
  168. 				}
  169. 			}
  170.  
  171.  
  172. 			// Open the URL with the list of available properties
  173. 			if ( openurl )
  174. 			{
  175. 				Process process = new Process( );
  176. 				process.StartInfo = new ProcessStartInfo( url );
  177. 				process.Start( );
  178. 			}
  179.  
  180.  
  181. 			return rc;
  182. 		}
  183.  
  184.  
  185. 		static string GetPowerStatus( )
  186. 		{
  187. 			string powerstatus = String.Empty;
  188. 			object propval = typeof( SystemInformation ).GetProperty( "PowerStatus" ).GetValue( typeof( SystemInformation ), null );
  189. 			string bcs = typeof( PowerStatus ).GetProperty( "BatteryChargeStatus" ).GetValue( propval, null ).ToString( );
  190. 			string bfl = typeof( PowerStatus ).GetProperty( "BatteryFullLifetime" ).GetValue( propval, null ).ToString( );
  191. 			string blp = ( Convert.ToInt32( typeof( PowerStatus ).GetProperty( "BatteryLifePercent" ).GetValue( propval, null ) ) * 100 ).ToString( ) + "%";
  192. 			string blr = typeof( PowerStatus ).GetProperty( "BatteryLifeRemaining" ).GetValue( propval, null ).ToString( );
  193. 			string pls = typeof( PowerStatus ).GetProperty( "PowerLineStatus" ).GetValue( propval, null ).ToString( );
  194. 			powerstatus = String.Format( "{{BatteryChargeStatus={0}", bcs );
  195. 			powerstatus += String.Format( ", BatteryFullLifetime={0}", ( bfl == "-1" ? "Unknown" : bfl ) );
  196. 			powerstatus += String.Format( ", BatteryLifePercent={0}", blp );
  197. 			powerstatus += String.Format( ", BatteryLifeRemaining={0}", ( blr == "-1" ? "Unknown" : blr ) );
  198. 			powerstatus += String.Format( ", PowerLineStatus={0}}}", pls );
  199. 			return powerstatus;
  200. 		}
  201.  
  202.  
  203. 		static int ShowHelp( params string[] errmsg )
  204. 		{
  205. 			#region Error Message
  206.  
  207. 			if ( errmsg.Length > 0 )
  208. 			{
  209. 				List<string> errargs = new List<string>( errmsg );
  210. 				errargs.RemoveAt( 0 );
  211. 				Console.Error.WriteLine( );
  212. 				Console.ForegroundColor = ConsoleColor.Red;
  213. 				Console.Error.Write( "ERROR:\t" );
  214. 				Console.ForegroundColor = ConsoleColor.White;
  215. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  216. 				Console.ResetColor( );
  217. 			}
  218.  
  219. 			#endregion Error Message
  220.  
  221.  
  222. 			#region Help Text
  223.  
  224. 			/*
  225. 			SystemInformation.exe,  Version 1.03
  226. 			Wrapper for the .NET SystemInformation class
  227.  
  228. 			Usage:   SystemInformation  [ property [ property [..] ] | /L ]  [ /I ]  [ /U ]
  229.  
  230. 			Where:   property   name or index of a SystemInformation property to be tested
  231. 			                    (multiple properties allowed, default: list all)
  232. 			         /I         Ignore and skip invalid property names specified on the
  233. 			                    command line (default: abort on invalid property name)
  234. 			         /L         List each property name and its index and value, sorted by
  235. 			                    name (default, implemented for backwards compatibility)
  236. 			         /U         open URL with list of properties and their meanings
  237.  
  238. 			Notes:   If a single property value is requested, the return code will equal
  239. 			         the value returned by the function if it is numerical, or 0 if not.
  240. 			         If multiple properties and their values are listed, return code is 0.
  241. 			         In case of (command line) errors the return code will be -1.
  242. 			         Be careful with the /I switch when requesting multiple properties,
  243. 			         as you will be unable to determine which particular one is ignored.
  244. 			         The meaning of the returned values can be found at
  245. 			         msdn.microsoft.com/library/system.windows.forms.systeminformation.aspx
  246.  
  247. 			Written by Rob van der Woude
  248. 			http://www.robvanderwoude.com
  249. 			*/
  250.  
  251. 			Console.Error.WriteLine( );
  252.  
  253. 			Console.Error.WriteLine( "SystemInformation.exe,  Version {0}", progver );
  254.  
  255. 			Console.Error.WriteLine( "Wrapper for the .NET SystemInformation class" );
  256.  
  257. 			Console.Error.WriteLine( );
  258.  
  259. 			Console.Error.Write( "Usage:   " );
  260. 			Console.ForegroundColor = ConsoleColor.White;
  261. 			Console.Error.WriteLine( "SystemInformation  [ property [ property [..] ] | /L ]  [ /I ]  [ /U ]" );
  262. 			Console.ResetColor( );
  263.  
  264. 			Console.Error.WriteLine( );
  265.  
  266. 			Console.Error.Write( "Where:   " );
  267. 			Console.ForegroundColor = ConsoleColor.White;
  268. 			Console.Error.Write( "property" );
  269. 			Console.ResetColor( );
  270. 			Console.Error.WriteLine( "   name or index of a SystemInformation property to be tested" );
  271.  
  272. 			Console.Error.WriteLine( "                    (multiple properties allowed, default: list all)" );
  273.  
  274. 			Console.ForegroundColor = ConsoleColor.White;
  275. 			Console.Error.Write( "         /I         I" );
  276. 			Console.ResetColor( );
  277. 			Console.Error.WriteLine( "gnore and skip invalid property names specified on the" );
  278.  
  279. 			Console.Error.WriteLine( "                    command line (default: abort on invalid property name)" );
  280.  
  281. 			Console.ForegroundColor = ConsoleColor.White;
  282. 			Console.Error.Write( "         /L         L" );
  283. 			Console.ResetColor( );
  284. 			Console.Error.WriteLine( "ist each property name and its index and value, sorted by" );
  285.  
  286. 			Console.Error.WriteLine( "                    name (default, implemented for backwards compatibility)" );
  287.  
  288. 			Console.ForegroundColor = ConsoleColor.White;
  289. 			Console.Error.Write( "         /U" );
  290. 			Console.ResetColor( );
  291. 			Console.Error.Write( "         open " );
  292. 			Console.ForegroundColor = ConsoleColor.White;
  293. 			Console.Error.Write( "U" );
  294. 			Console.ResetColor( );
  295. 			Console.Error.WriteLine( "RL with list of properties and their meanings" );
  296.  
  297. 			Console.Error.WriteLine( );
  298.  
  299. 			Console.Error.WriteLine( "Notes:   If a single property value is requested, the return code will equal" );
  300.  
  301. 			Console.Error.WriteLine( "         the value returned by the function if it is numerical, or 0 if not." );
  302.  
  303. 			Console.Error.WriteLine( "         If multiple properties and their values are listed, return code is 0." );
  304.  
  305. 			Console.Error.WriteLine( "         In case of (command line) errors the return code will be -1." );
  306.  
  307. 			Console.Error.WriteLine( "         Be careful with the /I switch when requesting multiple properties," );
  308.  
  309. 			Console.Error.WriteLine( "         as you will be unable to determine which particular one is ignored." );
  310.  
  311. 			Console.Error.WriteLine( "         The meaning of the returned values can be found online at" );
  312.  
  313. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  314. 			Console.Error.WriteLine( "         msdn.microsoft.com/library/system.windows.forms.systeminformation.aspx" );
  315. 			Console.ResetColor( );
  316.  
  317. 			Console.Error.WriteLine( );
  318.  
  319. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  320.  
  321. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  322.  
  323. 			#endregion Help Text
  324.  
  325.  
  326. 			return -1;
  327. 		}
  328. 	}
  329. }
  330.  

page last uploaded: 2017-08-21, 14:26