1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | public class Printer { /// <summary> /// 构造函数 /// </summary> /// <param name="name">打印机名称</param> public Printer( string name) { this .printer_name = name; } // 设备名:EPSON R330 Series private string _printer_name; /// <summary> /// 打印机名称 /// </summary> public string printer_name { get { return _printer_name; } set { _printer_name = value; } } /// <summary> /// 获取打印机状态 /// </summary> /// <returns></returns> public int getStatus() { string path = @"win32_printer.DeviceId='" + this .printer_name + "'" ; ManagementObject printer = new ManagementObject(path); printer.Get(); return Convert.ToInt32(printer.Properties[ "PrinterStatus" ].Value); } 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; } [DllImport( "winspool.Drv" , EntryPoint = "OpenPrinter" , SetLastError = true , CharSet = CharSet.Unicode, ExactSpelling = false , CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()] internal static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPTStr)] string printerName, out IntPtr phPrinter, ref structPrinterDefaults pd); [DllImport( "winspool.Drv" , EntryPoint = "GetPrinterA" , SetLastError = true , ExactSpelling = true , CallingConvention = CallingConvention.StdCall)] internal static extern bool GetPrinter(IntPtr hPrinter, int dwLevel, IntPtr pPrinter, int dwBuf, out int dwNeeded); [DllImport( "winspool.Drv" , EntryPoint = "ClosePrinter" , SetLastError = true , CharSet = CharSet.Unicode, ExactSpelling = false , CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()] internal static extern bool ClosePrinter(IntPtr phPrinter); 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; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] internal struct structPrinterDefaults { [MarshalAs(UnmanagedType.LPTStr)] public String pDatatype; public IntPtr pDevMode; [MarshalAs(UnmanagedType.I4)] public int DesiredAccess; }; [FlagsAttribute] internal enum PrinterStatus { PRINTER_STATUS_BUSY = 0x00000200, PRINTER_STATUS_DOOR_OPEN = 0x00400000, PRINTER_STATUS_ERROR = 0x00000002, PRINTER_STATUS_INITIALIZING = 0x00008000, PRINTER_STATUS_IO_ACTIVE = 0x00000100, PRINTER_STATUS_MANUAL_FEED = 0x00000020, PRINTER_STATUS_NO_TONER = 0x00040000, PRINTER_STATUS_NOT_AVAILABLE = 0x00001000, PRINTER_STATUS_OFFLINE = 0x00000080, PRINTER_STATUS_OUT_OF_MEMORY = 0x00200000, PRINTER_STATUS_OUTPUT_BIN_FULL = 0x00000800, PRINTER_STATUS_PAGE_PUNT = 0x00080000, PRINTER_STATUS_PAPER_JAM = 0x00000008, PRINTER_STATUS_PAPER_OUT = 0x00000010, PRINTER_STATUS_PAPER_PROBLEM = 0x00000040, PRINTER_STATUS_PAUSED = 0x00000001, PRINTER_STATUS_PENDING_DELETION = 0x00000004, PRINTER_STATUS_PRINTING = 0x00000400, PRINTER_STATUS_PROCESSING = 0x00004000, PRINTER_STATUS_TONER_LOW = 0x00020000, PRINTER_STATUS_USER_INTERVENTION = 0x00100000, PRINTER_STATUS_WAITING = 0x20000000, PRINTER_STATUS_WARMING_UP = 0x00010000 } [StructLayout(LayoutKind.Sequential)] internal struct PRINTER_INFO_2 { public string pServerName; public string pPrinterName; public string pShareName; public string pPortName; public string pDriverName; public string pComment; public string pLocation; public IntPtr pDevMode; public string pSepFile; public string pPrintProcessor; public string pDatatype; public string pParameters; public IntPtr pSecurityDescriptor; public uint Attributes; public uint Priority; public uint DefaultPriority; public uint StartTime; public uint UntilTime; public uint Status; public uint cJobs; public uint AveragePPM; } } |
1 | string statu= Printer.GetPrinterStatus( "打印机名称" ); |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步