PrintJob的实际总页数(Win32_PrintJob)

时间:2017-10-02 20:33:00

标签: c# winapi printing wmi print-job-control

每次使用ManagementEventWatcher进行更改时,我都会查询Win32_PrintJob WMI类,我获取了有关它的数据,例如:Document,HostPrintQueue,JobId,JobStatus,TotalPages等。但TotalPages不是表示要打印的实际页数,似乎此刻获取这些数据仍然是printjob还没有完成处理并在那一刻处理多个页面进行打印但实际总数是其他数字,如何在完成处理后获得打印作业的实际数量? 这是我的代码:

 ManagementEventWatcher createPrintJobWatcher;
        String strComputerName = ".";
        // Create event query to be notified within 1 milli second of a change in a service
        WqlEventQuery createPrintJobQuery = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 0.001 WHERE TargetInstance ISA \"Win32_PrintJob\"");

        createPrintJobWatcher = new ManagementEventWatcher();
        createPrintJobWatcher.Scope = new ManagementScope("\\\\" + strComputerName + "\\root\\CIMV2");
        createPrintJobWatcher.Query = createPrintJobQuery;
        // times out watcher.WaitForNextEvent in 1 seconds
        createPrintJobWatcher.Options.Timeout = new TimeSpan(0, 0, 1);
        //set the print event handler
        createPrintJobWatcher.EventArrived += new EventArrivedEventHandler(createPrintJobListener);

        createPrintJobWatcher.Start();

        Console.WriteLine("Listening...");

        Console.ReadLine();

createPrintJobListener方法:

        static void createPrintJobListener(object sender, EventArrivedEventArgs e)
    {

        SelectQuery query = new SelectQuery("Win32_PrintJob");
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        using (ManagementObjectCollection printJobs = searcher.Get())
            foreach (ManagementObject printJob in printJobs)
            {
                Console.WriteLine("c1:", printJob);
                Console.WriteLine("ID: {0}", printJob.GetPropertyValue("JobId").ToString());
                Console.WriteLine("name: {0}", printJob.GetPropertyValue("name").ToString());
                Console.WriteLine("status: {0}", printJob.GetPropertyValue("status").ToString());
                if (printJob.GetPropertyValue("JobStatus") != null)
                {
                    Console.WriteLine("JobStatus: {0}", printJob.GetPropertyValue("JobStatus").ToString());
                }
                else
                {
                    Console.WriteLine("JobStatus: NULLLLLL");
                }

                Console.WriteLine("PC: {0}", printJob.GetPropertyValue("HostPrintQueue").ToString());
                Console.WriteLine("TOTOAL PAGES: {0}", printJob.GetPropertyValue("TotalPages").ToString());                    
            }
    }

1 个答案:

答案 0 :(得分:0)

WMI可能是not sufficient to do this

Windows无法可靠地提供页数(或副本等),因此获取准确信息的唯一方法是暂停作业并解析它。这是一项非常重要的任务,但是here's a little more info