C#使用控制台列出当前所有可用的打印机列表
程序员文章站
2023-11-06 21:14:16
本文实例讲述了c#使用控制台列出当前所有可用打印机列表的方法。分享给大家供大家参考。具体如下:
// the initial c# code for the wm...
本文实例讲述了c#使用控制台列出当前所有可用打印机列表的方法。分享给大家供大家参考。具体如下:
// the initial c# code for the wmi query was generated by wmi code //generator, version 5.00, http://www.robvanderwoude.com/wmigen.php using system; using system.management; using system.collections; namespace robvanderwoude { public class listprinters { public static int main( string[] args ) { try { string computer = string.empty; #region command line parsing // only 1 optional argument allowed: a remote computer name if ( args.length > 1 ) { throw new exception( "invalid command line arguments" ); } if ( args.length == 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( string.empty ); 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 searcher.get( ) ) { printers.add( queryobj["deviceid"] ); } printers.sort( ); foreach ( string printer in printers ) { console.writeline( printer ); } return 0; } catch ( exception e ) { return writeerror( e ); } } public static int writeerror( exception e ) { return writeerror( e == null ? null : e.message ); } public static int writeerror( string errormessage ) { string fullpath = environment.getcommandlineargs( ).getvalue( 0 ).tostring( ); string[] program = fullpath.split( '\\' ); string exename = program[program.getupperbound( 0 )]; exename = exename.substring( 0, exename.indexof( '.' ) ); if ( string.isnullorempty( errormessage ) == false ) { console.error.writeline( ); console.foregroundcolor = consolecolor.red; console.error.write( "error: " ); console.foregroundcolor = consolecolor.white; console.error.writeline( errormessage ); console.resetcolor( ); } console.error.writeline( ); console.error.writeline( exename + ", version 1.10" ); console.error.writeline( "list all local printers on the specified computer" ); console.error.writeline( ); console.error.write( "usage: " ); console.foregroundcolor = consolecolor.white; console.error.write( exename.toupper( ) ); console.error.writeline( " [ computername ]" ); console.resetcolor( ); console.error.writeline( ); console.error.writeline( "where: 'computername' is the (optional) name of a remote computer" ); console.error.writeline( " (default if not specified: local computer)" ); console.error.writeline( ); console.error.writeline( "written by rob van der woude" ); return 1; } } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: 关羽不是有后代子女吗 那他的老婆到底是谁