如何检查打印机是否安装并准备好使用C#?

前端之家收集整理的这篇文章主要介绍了如何检查打印机是否安装并准备好使用C#?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何以编程方式检查打印机是否已安装(如果有的话,如何检查是否已经准备就绪)?在C#中使用.NET 3.5和Visual Studio 2008?

提前致谢,

解决方法

代码段将检索有关已安装打印机的信息:
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();
    }
  }

另一个选项是使用WMI.右键单击项目>添加参考>选择.NET选项卡>系统管理

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;

    }

这是另一个显示更多属性的片段:

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,"LastErrorCode");
            PrintProps(printer,"PrinterState");
            PrintProps(printer,"PrinterStatus");
            PrintProps(printer,"Status");
            PrintProps(printer,"WorkOffline");
            PrintProps(printer,"Local");
        }

    }
原文链接:https://www.f2er.com/csharp/96049.html

猜你在找的C#相关文章