獲取印表機當前的狀態
阿新 • • 發佈:2019-02-09
/// <summary>
/// 獲取印表機的當前狀態
/// </summary>
/// <param name="PrinterDevice">印表機裝置名稱</param>
/// <returns>印表機狀態</returns>
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;
}
/// <summary>
/// 印表機狀態
/// </summary>
public enum PrinterStatusBase
{
NotFound,
OtherState,
Unknown,
Ready,
OnPrint,
Prepare,
StopPrint,
Printing,
Offline,
}
/// <summary>
/// 印表機狀態
/// </summary>
[Flags]
public enum PrinterStatus
{
/// <summary>
/// 印表機就緒
/// </summary>
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,
/// <summary>
/// 列印中
/// </summary>
Printing = 0x00000400,
Processing = 0x00004000,
Toner_Low = 0x00020000,
User_Intervention = 0x00100000,
Waiting = 0x20000000,
Warming_Up = 0x00010000,
Other = 0x00000005
}
/// 獲取印表機的當前狀態
/// </summary>
/// <param name="PrinterDevice">印表機裝置名稱</param>
/// <returns>印表機狀態</returns>
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;
}
/// <summary>
/// 印表機狀態
/// </summary>
public enum PrinterStatusBase
{
NotFound,
OtherState,
Unknown,
Ready,
OnPrint,
Prepare,
StopPrint,
Printing,
Offline,
}
/// <summary>
/// 印表機狀態
/// </summary>
[Flags]
public enum PrinterStatus
{
/// <summary>
/// 印表機就緒
/// </summary>
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,
/// <summary>
/// 列印中
/// </summary>
Printing = 0x00000400,
Processing = 0x00004000,
Toner_Low = 0x00020000,
User_Intervention = 0x00100000,
Waiting = 0x20000000,
Warming_Up = 0x00010000,
Other = 0x00000005
}