如何使用 C# 检查打印机是否已安装并准备就绪?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1622903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do i check if a printer is installed and ready using C#?
提问by nithi
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?
我如何使用 .NET 3.5 和 Visual Studio 2008 在 C# 中以编程方式检查是否安装了打印机(如果有,我如何检查它是否已打开并准备使用?)?
Thanks in advance,
提前致谢,
回答by Mitch Wheat
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
另一种选择是使用 WMI。右键单击项目 > 添加引用 > 选择 .NET 选项卡 > 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");
}
}