找到了一些方法获取打印机的状态,但是结果不对。
1.WMI的方式
1 /// <summary> 2 /// 获取打印机的当前状态 3 /// </summary> 4 /// <param name="PrinterDevice">打印机设备名称</param> 5 /// <returns>打印机状态</returns> 6 public static PrinterStatus GetPrinterStatus(string PrinterDevice) 7 { 8 PrinterStatus ret = 0; 9 string path = @"win32_printer.DeviceId='" + PrinterDevice + "'"; 10 ManagementObject printer = new ManagementObject(path); 11 printer.Get(); 12 ret = (PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value); 13 return ret; 14 }
2.Windows API的方式
1 [DllImport("winspool.Drv", EntryPoint = "OpenPrinter", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()] 2 internal static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPTStr)]string printerName, out IntPtr phPrinter, ref structPrinterDefaults pd); 3 4 [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()] 5 internal static extern bool ClosePrinter(IntPtr phPrinter); 6 [DllImport("winspool.Drv", EntryPoint = "GetPrinterA", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] 7 internal static extern bool GetPrinter(IntPtr hPrinter, int dwLevel, IntPtr pPrinter, int dwBuf, out int dwNeeded); 8 9 internal static int GetPrinterStatusInt(string PrinterName) 10 { 11 int intRet = 0; 12 IntPtr hPrinter; 13 structPrinterDefaults defaults = new structPrinterDefaults(); 14 15 if (OpenPrinter(PrinterName, out hPrinter, ref defaults)) 16 { 17 int cbNeeded = 0; 18 bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded); 19 if (cbNeeded > 0) 20 { 21 IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded); 22 bolRet = GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded); 23 if (bolRet) 24 { 25 PRINTER_INFO_2 Info2 = new PRINTER_INFO_2(); 26 27 Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2)); 28 29 intRet = System.Convert.ToInt32(Info2.Status); 30 } 31 Marshal.FreeHGlobal(pAddr); 32 } 33 ClosePrinter(hPrinter); 34 } 35 36 return intRet; 37 } 38 39 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 40 internal struct structPrinterDefaults 41 { 42 [MarshalAs(UnmanagedType.LPTStr)] 43 public String pDatatype; 44 public IntPtr pDevMode; 45 [MarshalAs(UnmanagedType.I4)] 46 public int DesiredAccess; 47 }; 48 49 [StructLayout(LayoutKind.Sequential)] 50 internal struct PRINTER_INFO_2 51 { 52 public string pServerName; 53 public string pPrinterName; 54 public string pShareName; 55 public string pPortName; 56 public string pDriverName; 57 public string pComment; 58 public string pLocation; 59 public IntPtr pDevMode; 60 public string pSepFile; 61 public string pPrintProcessor; 62 public string pDatatype; 63 public string pParameters; 64 public IntPtr pSecurityDescriptor; 65 public uint Attributes; 66 public uint Priority; 67 public uint DefaultPriority; 68 public uint StartTime; 69 public uint UntilTime; 70 public uint Status; 71 public uint cJobs; 72 public uint AveragePPM; 73 }
明明是脱机状态,以上方法得到的都是就绪的状态,求指点。
string path = @"win32_printer.DeviceId='" + printerDevice + "'";
ManagementObject printer = new ManagementObject(path);
printer.Get();
ret = !(printer["WorkOffline"].ToString().ToLower().Equals("true"));