///
/// 获取打印机的当前状态
///
///
打印机设备名称
///
打印机状态
public static PrinterStatus GetPrinterState(string PrinterName)
{
PrinterStatus vStatus = PrinterStatus.Offline;
if (!StringHelper.IsEmpty(PrinterName))
{
try
{
string path = @"win32_printer.DeviceId='" + PrinterName + "'";
ManagementObject printer = new ManagementObject(path);
printer.Get();
bool isOffline = StringHelper.GetBoolean(printer.Properties["WorkOffline"].Value);
if (!isOffline)
{
var baseStatus = (PrinterStatusBase)Enum.Parse(typeof(PrinterStatusBase), printer.Properties["PrinterStatus"].Value.ToString());
if (baseStatus == PrinterStatusBase.Printing)
{
vStatus = PrinterStatus.Printing;
}
else if (baseStatus == PrinterStatusBase.NotFound)
{
vStatus = PrinterStatus.Offline;
}
else if (baseStatus == PrinterStatusBase.Ready)
{
vStatus = PrinterStatus.Ready;
}
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex);
}
}
return vStatus;
}
///
/// 打印机状态
///
public enum PrinterStatusBase
{
NotFound,
OtherState,
Unknown,
Ready,
OnPrint,
Prepare,
StopPrint,
Printing,
Offline,
}
///
/// 打印机状态
///
[Flags]
public enum PrinterStatus
{
///
/// 打印机就绪
///
Ready = 0x00000000,
Busy = 0x00000200,
Open = 0x00400000,
Error = 0x00000002,
Initializing = 0x00008000,
Io_Active = 0x00000100,
Manual_Feed = 0x00000020,
No_Toner = 0x00040000,
Not_Available = 0x00001000,
Offline = 0x00000080,
Out_Of_Menory = 0x00200000,
Output_Bin_Full = 0x00000800,
Page_Punt = 0x00080000,
Paper_Jam = 0x00000008,
Paper_Out = 0x00000010,
Paper_Problem = 0x00000040,
Paused = 0x00000001,
Pending_Deletion = 0x00000004,
///
/// 打印中
///
Printing = 0x00000400,
Processing = 0x00004000,
Toner_Low = 0x00020000,
User_Intervention = 0x00100000,
Waiting = 0x20000000,
Warming_Up = 0x00010000,
Other = 0x00000005
}