C # uses the console to list the current list of all available printers. Share to everyone for your reference. Specific as follows:
//the initial C # code for the WMI query is generated by WMI code//generator, Version 5.00, Http://www.robvanderwo ude.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 is requested &NBSP ; if (Args[0]. StartsWith ("/") | | Args[0]. StartsWith ("-")) { switch (args[0]. ToUpper ()) { case "/?": case "-?": case "H":  CA Se "-H": case "--h": case "/help": case " -help ": case"--help ": return Writeerror (string. Empty); default: return writeerror ("Invalid command line argument"); } &NBSP,} else { computer = "\\\ \ "+ args[0" + "\ \"; &NBSP,} #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 ()) {&nbsP printers. ADD (queryobj["DeviceID"]); printers. Sort (); foreach (string printer in Printers) { console.writeline (printer); &nb Sp } return 0; } catch (Exception e) { return Writeerror (e); } &nbs P } public static int writeerror (Exception e) { return writeerror (E = = null? null:e.message ); } public static int Writeerror (string errormessage) { string FullPath = environment.ge Tcommandlineargs (). 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 (); &nBsp 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; } }}
Codego.net Code
C # uses the console to list all currently available printers