日期:2014-05-18  浏览次数:21509 次

如何获取打印机将要打印的队列数?
如题。。。。
PrintServer myPrintServer = new PrintServer(); 
PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
网上都说是这段代码 但是我用了不行
有谁做过的?

------解决方案--------------------
先用平台调用,使用windows API:
C# code

        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEMTIME
        {
            public short wYear;
            public short wMonth;
            public short wDayOfWeek;
            public short wDay;
            public short wHour;
            public short wMinute;
            public short wSecond;
            public short wMilliseconds;
        }

        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern bool OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);

        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern bool ClosePrinter(IntPtr hPrinter);

        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern int EnumJobs(IntPtr hPrinter, int FirstJob, int NoJobs, int Level, IntPtr pInfo, int cdBuf, out int pcbNeeded, out int pcReturned);

        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern long GetJob(long hPrinter, long JobId, long Level, long buffer, long pbSize, long pbSizeNeeded);

        /// <summary>
        /// 得到具体打印机作业数
        /// </summary>
        /// <param name="printerToPeek">打印机名称</param>
        /// <returns></returns>
        public static int peekPrinterJobs(string printerToPeek)
        {
            IntPtr handle;
            int FirstJob = 0;
            int NumJobs = 127;
            int pcbNeeded;
            int pcReturned;

            // open printer 
            OpenPrinter(printerToPeek, out handle, IntPtr.Zero);

            // get num bytes required, here we assume the maxt job for the printer quest is 128 (0..127) 
            EnumJobs(handle, FirstJob, NumJobs, 1, IntPtr.Zero, 0, out pcbNeeded, out pcReturned);


            // allocate unmanaged memory 
            IntPtr pData = Marshal.AllocHGlobal(pcbNeeded);

            // get structs 
            EnumJobs(handle, FirstJob, NumJobs, 1, pData, pcbNeeded, out pcbNeeded, out pcReturned);

            //Close the Printer
            ClosePrinter(handle);

            return pcReturned;
        }

------解决方案--------------------
版本的类库:net3.5以上:
C# code

   PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues(); 
   String printQueueNames = "My Print Queues:\n\n"; 

   foreach (PrintQueue pq in myPrintQueues) 
   { 
      printQueueNames += "\t" + pq.Name + "\n"; 
   } 
   Console.WriteLine(printQueueNames); 
   Console.WriteLine("\nPress Return to continue."); 
   Console.ReadLine();