Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for getsysteminformation.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Forms;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	internal class GetSystemInformation
  11. 	{
  12. 		static string progver = "1.02";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			int columnwidth = 0;
  18. 			SortedList<string, object> sysinfolist = new SortedList<string, object>( );
  19.  
  20. 			Type sysinfotype = typeof( SystemInformation );
  21. 			PropertyInfo[] propertyinfo = sysinfotype.GetProperties( );
  22.  
  23. 			for ( int i = 0; i < propertyinfo.Length; i++ )
  24. 			{
  25. 				string name = propertyinfo[i].Name;
  26. 				// populate list with property names and values
  27. 				sysinfolist[name] = propertyinfo[i].GetValue( sysinfotype, null );
  28. 				// calculate required column width
  29. 				columnwidth = Math.Max( columnwidth, name.Length );
  30. 			}
  31.  
  32. 			switch ( args.Length )
  33. 			{
  34. 				case 0:
  35. 					// show entire list
  36. 					foreach ( string key in sysinfolist.Keys )
  37. 					{
  38. 						Console.WriteLine( "{0,-" + columnwidth + "}    {1}", key, sysinfolist[key] );
  39. 					}
  40. 					return 0;
  41. 				case 1:
  42. 					if ( args[0].Contains( "?" ) )
  43. 					{
  44. 						return ShowHelp( );
  45. 					}
  46. 					// test exact match, no wildcards
  47. 					foreach ( string key in sysinfolist.Keys )
  48. 					{
  49. 						if ( key.ToUpper( ) == args[0].ToUpper( ) ) // make the search case insensitive
  50. 						{
  51. 							// show matching property and its value
  52. 							Console.WriteLine( "{0}:    {1}", key, sysinfolist[key] );
  53. 							string testvalue = string.Format( "{0}", sysinfolist[key] );
  54. 							int rc;
  55. 							// return code equals numeric value, if possible
  56. 							if ( int.TryParse( testvalue, out rc ) )
  57. 							{
  58. 								return rc;
  59. 							}
  60. 							return 0;
  61. 						}
  62. 					}
  63. 					// test wildcard matches
  64. 					if ( args[0].Contains( "*" ) )
  65. 					{
  66. 						bool matchfound = false;
  67. 						// exclude partial matches
  68. 						string pattern = string.Format( "^{0}$", args[0] );
  69. 						// convert "DOS format" wildcards to regex format
  70. 						pattern = pattern.Replace( "*", ".*?" );
  71. 						Regex regex = new Regex( pattern, RegexOptions.IgnoreCase );
  72. 						// recalculate required column width
  73. 						columnwidth = 0;
  74. 						foreach ( string key in sysinfolist.Keys )
  75. 						{
  76. 							if ( regex.IsMatch( key ) )
  77. 							{
  78. 								matchfound = true;
  79. 								columnwidth = Math.Max( columnwidth, key.Length );
  80. 							}
  81. 						}
  82. 						if ( !matchfound )
  83. 						{
  84. 							return ShowHelp( "Invalid command line argument \"{0}\"", args[0] );
  85. 						}
  86. 						// show all matching properties and their values
  87. 						foreach ( string key in sysinfolist.Keys )
  88. 						{
  89. 							if ( regex.IsMatch( key ) )
  90. 							{
  91. 								matchfound = true;
  92. 								Console.WriteLine( "{0,-" + columnwidth + "}    {1}", key, sysinfolist[key] );
  93. 							}
  94. 						}
  95. 						return 0;
  96. 					}
  97. 					else
  98. 					{
  99. 						return ShowHelp( "Invalid command line argument \"{0}\"", args[0] );
  100. 					}
  101. 				default:
  102. 					return ShowHelp( "Too many command line arguments" );
  103. 			}
  104. 		}
  105.  
  106.  
  107. 		static int ShowHelp( params string[] errmsg )
  108. 		{
  109. 			#region Error Message
  110.  
  111. 			if ( errmsg.Length > 0 )
  112. 			{
  113. 				List<string> errargs = new List<string>( errmsg );
  114. 				errargs.RemoveAt( 0 );
  115. 				Console.Error.WriteLine( );
  116. 				Console.ForegroundColor = ConsoleColor.Red;
  117. 				Console.Error.Write( "ERROR:\t" );
  118. 				Console.ForegroundColor = ConsoleColor.White;
  119. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  120. 				Console.ResetColor( );
  121. 			}
  122.  
  123. 			#endregion Error Message
  124.  
  125. 			#region Help Text
  126.  
  127. 			/*
  128. 			GetSystemInformation.exe,  Version 1.02
  129. 			Get properties and values of .NET's System.Windows.Forms.SystemInformation class
  130.  
  131. 			Usage:    GetSystemInformation.exe  [ property ]
  132.  
  133. 			Where:    property     is the name of the property (or properties) to be queried
  134. 			                       (wildcard * allowed, e.g. mouse* or *window*)
  135.  
  136. 			Notes:    Without a command line argument, the program will list all properties
  137. 			          and values of .NET's System.Windows.Forms.SystemInformation class.
  138. 			          If a single property name (no wildcards) is specified on the command
  139. 			          line, and its value is numeric, the return code will equal (the
  140. 			          integer of) that value, or 0 if the value isn't numeric, or -1 in
  141. 			          case of (command line) errors.
  142.  
  143. 			Written by Rob van der Woude
  144. 			https://www.robvanderwoude.com
  145. 			*/
  146.  
  147. 			Console.Error.WriteLine( );
  148.  
  149. 			Console.Error.WriteLine( "GetSystemInformation.exe,  Version {0}", progver );
  150.  
  151. 			Console.Error.WriteLine( "Get properties and values of .NET's System.Windows.Forms.SystemInformation class" );
  152.  
  153. 			Console.Error.WriteLine( );
  154.  
  155. 			Console.Error.Write( "Usage:    " );
  156. 			Console.ForegroundColor = ConsoleColor.White;
  157. 			Console.Error.WriteLine( "GetSystemInformation.exe  [ property ]" );
  158. 			Console.ResetColor( );
  159.  
  160. 			Console.Error.WriteLine( );
  161.  
  162. 			Console.Error.Write( "Where:    " );
  163. 			Console.ForegroundColor = ConsoleColor.White;
  164. 			Console.Error.Write( "property" );
  165. 			Console.ResetColor( );
  166. 			Console.Error.WriteLine( "     is the name of the property (or properties) to be queried" );
  167.  
  168. 			Console.Error.WriteLine( "                       (wildcard * allowed, e.g. mouse* or *window*)" );
  169.  
  170. 			Console.Error.WriteLine( );
  171.  
  172. 			Console.Error.WriteLine( "Notes:    Without a command line argument, the program will list all properties" );
  173.  
  174. 			Console.Error.WriteLine( "          and values of .NET's System.Windows.Forms.SystemInformation class." );
  175.  
  176. 			Console.Error.WriteLine( "          If a single property name (no wildcards) is specified on the command" );
  177.  
  178. 			Console.Error.WriteLine( "          line, and its value is numeric, the return code will equal (the" );
  179.  
  180. 			Console.Error.WriteLine( "          integer of) that value, or 0 if the value isn't numeric, or -1 in" );
  181.  
  182. 			Console.Error.WriteLine( "          case of (command line) errors." );
  183.  
  184. 			Console.Error.WriteLine( );
  185.  
  186. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  187.  
  188. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  189.  
  190. 			#endregion Help Text
  191.  
  192. 			return -1;
  193. 		}
  194. 	}
  195. }

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