使用 c# 和 WMI 计算客​​户端的打印页数 [英] Counting printed Pages by Clients with c# and WMI

查看:35
本文介绍了使用 c# 和 WMI 计算客​​户端的打印页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标就像我在主题中所说的那样.我知道有很多关于该特定问题的文章,我尝试了所有文章.但是由于它们都不适合我,我现在试图找出为什么这个有时有效,有时尽管打印了很多东西,但什么也没有发生.所以这是我的代码,现在应该等待打印作业并告诉我它.而已.

the goal is like i said in the Topic. I know there are a lot of articles on that specific Problem and i tried all most all of them. But since non of them worked out for me, I'm now trying to find out why this one just works sometimes and sometimes nothing is happening although many things are printed. So this is my code which right now should wait for a job to be printed and just tell me about it. Nothing more.

private void StartMonitor()
    {

        try
        {
            var opt = new ConnectionOptions { EnablePrivileges = true };

            var scope = new ManagementScope("root\\CIMV2", opt);

            scope.Connect();

            var query = new WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 60 WHERE TargetInstance ISA \"Win32_PrintJob\"");

            var watcher = new ManagementEventWatcher(query);

            Console.WriteLine("Ready to receive Printer Job events...");

            var pjEvent = watcher.WaitForNextEvent();

            if (pjEvent != null) Console.WriteLine("Event occured: " + pjEvent.Properties["PagesPrinted"]);
        }
        catch (ManagementException e)
        {
            Console.WriteLine(e.StackTrace);
            Console.WriteLine(e.ErrorCode);
            Console.WriteLine(e.ErrorInformation);
            _Error = e.Message;
            throw;
        }
    }

推荐答案

要获得作业打印的总页数,我会尝试监视 __InstanceDeletionEvents,我相信那时 Win32_PrintJob.TotalPages 应该准确显示打印的页数(可以'现在就测试一下,抱歉).这里还有一个不错的选择:

To get the total pages printed by a job I would try monitoring __InstanceDeletionEvents, I believe that at that time Win32_PrintJob.TotalPages should show the printed pages accurately (can't test this right now, sorry). There is also a nice alternative here:

从 VB.NET 监控打印机队列

如果你看文章评论,作者还建议监控 JOB_WRITTEN 事件以获得打印的总页数.

If you look at the article comments, the author also advises to monitor JOB_WRITTEN events to get the total number of pages printed.

如果您正在尝试监控大型打印作业的进度,请在创建作业后尝试监控 __InstanceModificationEvents,如下所示:

If you are trying to monitor the progress of large print jobs, try monitoring __InstanceModificationEvents once the job is created, something like this:

Select * From __InstanceModificationEvent Within 1 
Where TargetInstance Isa "Win32_PrintJob" 
And TargetInstance.PagesPrinted > PreviousInstance.PagesPrinted

这篇关于使用 c# 和 WMI 计算客​​户端的打印页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆