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

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

概述如何以编程方式检查打印机是否已安装(如果有的话,如何检查是否已经准备就绪)?在C#中使用.NET 3.5和Visual Studio 2008? 提前致谢, 此代码段将检索有关已安装打印机的信息: using System.Drawing.Printing;//... foreach (string printerName in PrinterSettings.InstalledPrint 如何以编程方式检查打印机是否已安装(如果有的话,如何检查是否已经准备就绪)?在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");        }    }
总结

以上是内存溢出为你收集整理的如何检查打印机是否安装并准备好使用C#?全部内容,希望文章能够帮你解决如何检查打印机是否安装并准备好使用C#?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1256172.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存