如何在 C# .NET 中捕获打印作业 [英] How to catch print jobs in C# .NET
问题描述
我试图在我们的本地网络中捕获提交给打印机的每个打印作业.我想显示作业的一些属性,如作业名称、提交时间等.
I am trying to catch every print jobs submitted to printer in our local network. I want to display some properties of the job like job name, submit time and so on.
我尝试了一个 while 循环,但它没有抓住我的打印作业,可能是因为它发生在线程休眠时.是否有我可以注册和处理的事件?我不想为这个任务无限循环地花费所有的 CPU 资源.
I tried a while loop but it didn't catch my print job, maybe beacuse it happened while the thread was sleeping. Is there an event that I can register and handle? I don't want to spend all of the CPU resource for this task infinetly looping.
我试过了:
public static void WritePrinterJobs()
{
while (true)
{
foreach (var job in LocalPrintServer.GetDefaultPrintQueue().GetPrintJobInfoCollection())
{
Console.WriteLine(job.Submitter + " " + job.TimeJobSubmitted.ToShortDateString());
}
Thread.Sleep(100);
}
}
上面的代码实际上有效,如果它对你有效,你不需要降低级别,我的错误是没有正确配置默认打印机.
The code above actually works, you don't need to go lower level if it does work for you, my mistake was not configuring default printer correctly.
推荐答案
我找到了解决方案.对于那些正在寻找处理打印作业事件的方法的人,请查看:
I found the solution. For those are searching for a way to handle print job events take a look:
FindFirstPrinterChangeNotification
还可以找到工作代码监控打印作业
这篇关于如何在 C# .NET 中捕获打印作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!