Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

winforms - how do i check if a printer is installed and ready using C#?

How do i programmatically check if a printer is installed or not (and if there is one, how do i check if it is on and ready to use?) in C# using .NET 3.5 and Visual Studio 2008?

Thanks in advance,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This snippet will retrieve information about installed printers:

using System.Drawing.Printing;
//...

  foreach (string printerName in PrinterSettings.InstalledPrinters)
  {
    // Display the printer name.
    Console.WriteLine("Printer: {0}", printerName);

    // Retrieve the printer settings.
    PrinterSettings printer = new PrinterSettings();
    printer.PrinterName = printerName;

    // Check that this is a valid printer.
    // (This step might be required if you read the printer name
    // from a user-supplied value or a registry or configuration file
    // setting.)
    if (printer.IsValid)
    {
      // Display the list of valid resolutions.
      Console.WriteLine("Supported Resolutions:");

      foreach (PrinterResolution resolution in
        printer.PrinterResolutions)
      {
        Console.WriteLine("  {0}", resolution);
      }
      Console.WriteLine();

      // Display the list of valid paper sizes.
      Console.WriteLine("Supported Paper Sizes:");

      foreach (PaperSize size in printer.PaperSizes)
      {
        if (Enum.IsDefined(size.Kind.GetType(), size.Kind))
        {
          Console.WriteLine("  {0}", size);
        }
      }
      Console.WriteLine();
    }
  }

The other option is to use WMI. Right Click Project > Add Reference > Select .NET Tab > System.Management

using System.Management;
// ...

   private List<string> GetPrinters()
   {     
        List<string> printerNames = new List<string>();

        // Use the ObjectQuery to get the list of configured printers
        System.Management.ObjectQuery oquery = 
            new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");

        System.Management.ManagementObjectSearcher mosearcher = 
            new System.Management.ManagementObjectSearcher(oquery);

        System.Management.ManagementObjectCollection moc = mosearcher.Get();

        foreach (ManagementObject mo in moc)
        {
            System.Management.PropertyDataCollection pdc = mo.Properties;
            foreach (System.Management.PropertyData pd in pdc)
            {
                if ((bool)mo["Network"])
                {
                    printerNames.Add(mo[pd.Name]);
                }
            }
        }

        return printerNames;

    }

Here's another snippet which shows more properties:

    static void PrintProps(ManagementObject o, string prop)
    {
        try { Console.WriteLine(prop + "|" + o[prop]); }
        catch (Exception e) { Console.Write(e.ToString()); }
    }

    static void Main(string[] args)
    {

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");

        foreach (ManagementObject printer in searcher.Get())
        {
            string printerName = printer["Name"].ToString().ToLower();
            Console.WriteLine("Printer :" + printerName);

            PrintProps(printer, "Caption");
            PrintProps(printer, "ExtendedPrinterStatus");
            PrintProps(printer, "Availability");
            PrintProps(printer, "Default");
            PrintProps(printer, "DetectedErrorState");
            PrintProps(printer, "ExtendedDetectedErrorState");
            PrintProps(printer, "ExtendedPrinterStatus");
            PrintProps(printer, "LastErrorCode");
            PrintProps(printer, "PrinterState");
            PrintProps(printer, "PrinterStatus");
            PrintProps(printer, "Status");
            PrintProps(printer, "WorkOffline");
            PrintProps(printer, "Local");
        }

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...