C # uses the console to list all currently available printers

Source: Internet
Author: User

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 &nbsp ;   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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.