(view source code of getsysteminformation.cs as plain text)
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RobvanderWoude
{
internal class GetSystemInformation
{
static string progver = "1.02";
static int Main( string[] args )
{
int columnwidth = 0;
SortedList<string, object> sysinfolist = new SortedList<string, object>( );
Type sysinfotype = typeof( SystemInformation );
PropertyInfo[] propertyinfo = sysinfotype.GetProperties( );
for ( int i = 0; i < propertyinfo.Length; i++ )
{
string name = propertyinfo[i].Name;
// populate list with property names and values
sysinfolist[name] = propertyinfo[i].GetValue( sysinfotype, null );
// calculate required column width
columnwidth = Math.Max( columnwidth, name.Length );
}
switch ( args.Length )
{
case 0:
// show entire list
foreach ( string key in sysinfolist.Keys )
{
Console.WriteLine( "{0,-" + columnwidth + "} {1}", key, sysinfolist[key] );
}
return 0;
case 1:
if ( args[0].Contains( "?" ) )
{
return ShowHelp( );
}
// test exact match, no wildcards
foreach ( string key in sysinfolist.Keys )
{
if ( key.ToUpper( ) == args[0].ToUpper( ) ) // make the search case insensitive
{
// show matching property and its value
Console.WriteLine( "{0}: {1}", key, sysinfolist[key] );
string testvalue = string.Format( "{0}", sysinfolist[key] );
int rc;
// return code equals numeric value, if possible
if ( int.TryParse( testvalue, out rc ) )
{
return rc;
}
return 0;
}
}
// test wildcard matches
if ( args[0].Contains( "*" ) )
{
bool matchfound = false;
// exclude partial matches
string pattern = string.Format( "^{0}$", args[0] );
// convert "DOS format" wildcards to regex format
pattern = pattern.Replace( "*", ".*?" );
Regex regex = new Regex( pattern, RegexOptions.IgnoreCase );
// recalculate required column width
columnwidth = 0;
foreach ( string key in sysinfolist.Keys )
{
if ( regex.IsMatch( key ) )
{
matchfound = true;
columnwidth = Math.Max( columnwidth, key.Length );
}
}
if ( !matchfound )
{
return ShowHelp( "Invalid command line argument \"{0}\"", args[0] );
}
// show all matching properties and their values
foreach ( string key in sysinfolist.Keys )
{
if ( regex.IsMatch( key ) )
{
matchfound = true;
Console.WriteLine( "{0,-" + columnwidth + "} {1}", key, sysinfolist[key] );
}
}
return 0;
}
else
{
return ShowHelp( "Invalid command line argument \"{0}\"", args[0] );
}
default:
return ShowHelp( "Too many command line arguments" );
}
}
static int ShowHelp( params string[] errmsg )
{
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
}
#endregion Error Message
#region Help Text
/*
GetSystemInformation.exe, Version 1.02
Get properties and values of .NET's System.Windows.Forms.SystemInformation class
Usage: GetSystemInformation.exe [ property ]
Where: property is the name of the property (or properties) to be queried
(wildcard * allowed, e.g. mouse* or *window*)
Notes: Without a command line argument, the program will list all properties
and values of .NET's System.Windows.Forms.SystemInformation class.
If a single property name (no wildcards) is specified on the command
line, and its value is numeric, the return code will equal (the
integer of) that value, or 0 if the value isn't numeric, or -1 in
case of (command line) errors.
Written by Rob van der Woude
https://www.robvanderwoude.com
*/
Console.Error.WriteLine( );
Console.Error.WriteLine( "GetSystemInformation.exe, Version {0}", progver );
Console.Error.WriteLine( "Get properties and values of .NET's System.Windows.Forms.SystemInformation class" );
Console.Error.WriteLine( );
Console.Error.Write( "Usage: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( "GetSystemInformation.exe [ property ]" );
Console.ResetColor( );
Console.Error.WriteLine( );
Console.Error.Write( "Where: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.Write( "property" );
Console.ResetColor( );
Console.Error.WriteLine( " is the name of the property (or properties) to be queried" );
Console.Error.WriteLine( " (wildcard * allowed, e.g. mouse* or *window*)" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Notes: Without a command line argument, the program will list all properties" );
Console.Error.WriteLine( " and values of .NET's System.Windows.Forms.SystemInformation class." );
Console.Error.WriteLine( " If a single property name (no wildcards) is specified on the command" );
Console.Error.WriteLine( " line, and its value is numeric, the return code will equal (the" );
Console.Error.WriteLine( " integer of) that value, or 0 if the value isn't numeric, or -1 in" );
Console.Error.WriteLine( " case of (command line) errors." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Help Text
return -1;
}
}
}
page last modified: 2024-04-16; loaded in 0.0094 seconds