This article shows how C# uses the console to list all currently available printers. Share it for your reference. The details are as follows:
// The initial C# code for the WMI query was generated by WMI Code //Generator, Version 5.00, / using System; using ; using ; namespace RobvanderWoude { public class ListPrinters { public static int Main( string[] args ) { try { string computer = ; #region Command line parsing // Only 1 optional argument allowed: a remote computer name if ( > 1 ) { throw new Exception( "Invalid command line arguments" ); } if ( == 1 ) { // We'll display a 'friendly' message if help was requested if ( args[0].StartsWith( "/" ) || args[0].StartsWith( "-" ) ) { switch ( args[0].ToUpper( ) ) { case "/?": case "-?": case "/H": case "-H": case "--H": case "/HELP": case "-HELP": case "--HELP": return WriteError( ); default: return WriteError( "Invalid command line argument" ); } } else { computer = "\\\\" + args[0] + "\\"; } } #endregion string wmins = computer + "root\\CIMV2"; ManagementObjectSearcher searcher = new ManagementObjectSearcher( wmins, "SELECT * FROM Win32_Printer" ); ArrayList printers = new ArrayList( ); foreach ( ManagementObject queryObj in ( ) ) { ( queryObj["DeviceID"] ); } ( ); foreach ( string printer in printers ) { ( printer ); } return 0; } catch ( Exception e ) { return WriteError( e ); } } public static int WriteError( Exception e ) { return WriteError( e == null ? null : ); } public static int WriteError( string errorMessage ) { string fullpath = ( ).GetValue( 0 ).ToString( ); string[] program = ( '\\' ); string exename = program[( 0 )]; exename = ( 0, ( '.' ) ); if ( ( errorMessage ) == false ) { ( ); = ; ( "ERROR: " ); = ; ( errorMessage ); ( ); } ( ); ( exename + ", Version 1.10" ); ( "List all local printers on the specified computer" ); ( ); ( "Usage: " ); = ; ( ( ) ); ( " [ computername ]" ); ( ); ( ); ( "Where: 'computername' is the (optional) name of a remote computer" ); ( " (default if not specified: local computer)" ); ( ); ( "Written by Rob van der Woude" ); return 1; } } }
I hope this article will be helpful to everyone's C# programming.