//https://www.cnblogs.com/gordensong/p/3825462.html
public class Printer
{
///
/// 获取打印机状态,是否离线。
///
///
///
public static bool WorkOffline(string printerName)
{
string query = string.Format("SELECT * from Win32_Printer ");
var searcher = new ManagementObjectSearcher(query);
var printerzs = searcher.Get();
foreach (var printer in printerzs)
{
string printername = printer.Properties["Name"].Value.ToString();
if (printername == printerName)
{
string workOffline = printer.Properties["WorkOffline"].Value.ToString().ToLower();
return workOffline != "false";
}
}
return false;
}
private Printer()
{
}
internal static int GetPrinterStatusInt(string PrinterName)
{
int intRet = 0;
IntPtr hPrinter;
structPrinterDefaults defaults = new structPrinterDefaults();
if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
{
int cbNeeded = 0;
bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded);
if (cbNeeded > 0)
{
IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
bolRet = GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded);
if (bolRet)
{
PRINTER_INFO_2 Info2 = new PRINTER_INFO_2();
Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));
intRet = System.Convert.ToInt32(Info2.Status);
}
Marshal.FreeHGlobal(pAddr);
}
ClosePrinter(hPrinter);
}
return intRet;
}
internal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
{
uint cbNeeded = 0;
uint cReturned = 0;
bool ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);
IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);
if (ret)
{
PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];
int offset = pAddr.ToInt32();
for (int i = 0; i < cReturned; i++)
{
Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pPortName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pDriverName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pComment = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pLocation = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pDevMode = Marshal.ReadIntPtr(new IntPtr(offset));
offset += 4;
Info2[i].pSepFile = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pPrintProcessor = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pDatatype = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pParameters = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));
offset += 4;
Info2[i].Attributes = (uint)Marshal.ReadIntPtr(new IntPtr(offset));
offset += 4;
Info2[i].Priority = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].DefaultPriority = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].StartTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].UntilTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].Status = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].cJobs = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].AveragePPM = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
}
Marshal.FreeHGlobal(pAddr);
return Info2;
}
else
{
return new PRINTER_INFO_2[0];
}
}
///
/// 获取当前指定打印机的状态
///
/// 打印机名称
/// 打印机状态描述
public static string GetPrinterStatus(string PrinterName)
{
int intValue = GetPrinterStatusInt(PrinterName);
string strRet = string.Empty;
switch (intValue)
{
case 0:
strRet = "准备就绪(Ready)";
break;
case 0x00000200:
strRet = "忙(Busy)";
break;
case 0x00400000:
strRet = "门被打开(Printer Door Open)";
break;
case 0x00000002:
strRet = "错误(Printer Error)";
break;
case 0x0008000:
strRet = "正在初始化(Initializing)";
break;
case 0x00000100:
strRet = "正在输入或输出(I/O Active)";
break;
case 0x00000020:
strRet = "手工送纸(Manual Feed)";
break;
case 0x00040000:
strRet = "无墨粉(No Toner)";
break;
case 0x00001000:
strRet = "不可用(Not Available)";
break;
case 0x00000080:
strRet = "脱机(Off Line)";
break;
case 0x00200000:
strRet = "内存溢出(Out of Memory)";
break;
case 0x00000800:
strRet = "输出口已满(Output Bin Full)";
break;
case 0x00080000:
strRet = "当前页无法打印(Page Punt)";
break;
case 0x00000008:
strRet = "塞纸(Paper Jam)";
break;
case 0x00000010:
strRet = "打印纸用完(Paper Out)";
break;
case 0x00000040:
strRet = "纸张问题(Page Problem)";
break;
case 0x00000001:
strRet = "暂停(Paused)";
break;
case 0x00000004:
strRet = "正在删除(Pending Deletion)";
break;
case 0x00000400:
strRet = "正在打印(Printing)";
break;
case 0x00004000:
strRet = "正在处理(Processing)";
break;
case 0x00020000:
strRet = "墨粉不足(Toner Low)";
break;
case 0x00100000:
strRet = "需要用户干预(User Intervention)";
break;
case 0x20000000:
strRet = "等待(Waiting)";
break;
case 0x00010000:
strRet = "正在准备(Warming Up)";
break;
default:
strRet = "未知状态(Unknown Status)";
break;
}
return strRet;
}
///
/// 获取本地打印机列表
/// 可以通过制定参数获取网络打印机
///
/// 打印机列表
public static List GetPrinterList()
{
List list = new List();
PRINTER_INFO_2[] Info2 = EnumPrintersByFlag(PrinterEnumFlags.PRINTER_ENUM_LOCAL);
for (int i = 0; i < Info2.Length; i++)
{
list.Add(Info2[i].pPrinterName);
}
return list;
}
///
/// 获取安装的打印机
///
///
public static List GetInstalledPrinters()
{
List printers = new List();
foreach (object installedPrinter in PrinterSettings.InstalledPrinters)
{
string printer = installedPrinter.ToString();
printers.Add(printer);
}
return printers;
}
///
/// 获取本机的默认打印机名称
///
/// 默认打印机名称
public static string GetDeaultPrinterName()
{
StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;
if (GetDefaultPrinter(dp, ref size))
{
return dp.ToString();
}
else
{
int rc = GetLastError();
return string.Empty;
}
}
///
/// 设置默认打印机
///
/// 可用的打印机名称
public static void SetPrinterToDefault(string PrinterName)
{
SetDefaultPrinter(PrinterName);
}
/////
///// 判断打印机是否在系统可用的打印机列表中
/////
///// 打印机名称
///// 是:在;否:不在
public static bool PrinterInList(string PrinterName)
{
List alPrinters = GetPrinterList();
bool bolRet = alPrinters.Cast