此外,您可以检查扩展打印机状态和其他属性;有线打印机可以提供比无线打印机(Lan、WLan、蓝牙)更多的信息。
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printer
using System;
using System.Management;
using System.Windows.Forms;
namespace PrinterSet
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
QueryOnWMI();
}
private void QueryOnWMI()
{
try
{
// Common Information Model v2 (namespace)
string scope = @"root\CIMV2";
string printerName = printerNameTextBox.Text.Trim();
//printerName = "%DocuCentre%";
string query = "SELECT * FROM Win32_Printer";
if (!string.IsNullOrEmpty(printerName))
{
query += $" WHERE Name Like '%{printerName}%'";
}
Console.Clear();
var searcher = new ManagementObjectSearcher(scope, query);
var result = searcher.Get();
if (result == null || result.Count == 0)
{
Console.WriteLine($"Printer '{printerName}' not found");
}
var line = new string('-', 40);
foreach (ManagementObject queryObj in result)
{
Console.WriteLine(line);
Console.WriteLine($"Printer : {queryObj["Name"]}");
ushort ps = Convert.ToUInt16(queryObj["PrinterStatus"]);
var psEnum = (PrinterStatus)ps;
Console.WriteLine("PrinterStatus: {0} ({1})", psEnum, ps);
ps = Convert.ToUInt16(queryObj["ExtendedPrinterStatus"]);
var psExtended = (ExtendedPrinterStatus)ps;
Console.WriteLine("Extended Status: {0} ({1})", psExtended, ps);
//var papers = (string[])queryObj["PrinterPaperNames"];
//foreach (var paper in papers)
//{
// Console.WriteLine("Paper Name: {0}", paper);
//}
Console.WriteLine(line);
}
}
catch (ManagementException emx)
{
// TRY => NET STOP SPOOLER
// Generic failure is thrown
MessageBox.Show(this, "Invalid query: " + emx.Message);
}
}
public enum PrinterStatus : UInt16
{
Other = 1, Unknown = 2, Idle = 3, Printing= 4, Warmup = 5, StoppedPrinting = 6, Offline = 7,
}
public enum ExtendedPrinterStatus : UInt16
{
Other = 1, Unknown = 2, Idle = 3, Printing, WarmingUp, StoppedPrinting, Offline, Paused, Error,
Busy, NotAvailable, Waiting, Processing, Initialization, PowerSave, PendingDeletion, IOActive, ManualFeed
}
private void button1_Click(object sender, EventArgs e)
{
QueryOnWMI();
}
}
}
您还可以探索 Windows spooler API:
https ://docs.microsoft.com/en-us/windows-hardware/drivers/print/introduction-to-spooler-components
还有这个:
windows-printer-driver@stackoverflow
安东尼奥