如何使用c#获取打印作业状态

本文关键字:打印 作业状态 获取 何使用 | 更新日期: 2023-09-27 18:04:31

我可以打印文档,但是我不知道如何获得它的状态。我浏览了许多资源(MSDN,用于检查作业状态的链接),但未能找到答案。

我实际上想从打印机那里得到文档是否打印成功的确认。此外,我也很感兴趣,如果我能得到错误的信号,从打印机,如纸张卡纸。

我有我要发送打印的打印机名称和文档名称。有没有人在这方面做过一些研究,可以告诉我如何做到这一点?

如何使用c#获取打印作业状态

您可以使用WMI来完成此操作。它提供了几个打印相关的类,包括Win32_PrintJob。

这是未经测试的,但像这样的东西应该让你开始:

SelectQuery query = new SelectQuery("Win32_PrintJob");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
using (ManagementObjectCollection printJobs = searcher.Get())
    foreach (ManagementObject printJob in printJobs)
    {
        // The format of the Win32_PrintJob.Name property is "PrinterName,JobNumber"
        string name = (string) printJob["Name"];
        string[] nameParts = name.Split(',');
        string printerName = nameParts[0];
        string jobNumber = nameParts[1];
        string document = (string) printJob["Document"];
        string jobStatus = (string) printJob["JobStatus"];
        // Process job properties...
    }

当然,上面的答案是正确的,但是如果你在c#中使用Windows API来检查打印作业状态,你必须做更多一点,特别是要摆脱嵌套的if-then-else子句。下面是一个示例代码:

namespace Sample
{
    class UsingNativeAPIs
    {
        private const int JOB_STATUS_RETAINED = 8192;          
        private const int JOB_STATUS_COMPLETE = 4096;          
        private const int JOB_STATUS_RESTART = 2048;           
        private const int JOB_STATUS_USER_INTERVENTION = 1024;
        private const int JOB_STATUS_BLOCKED_DEVQ = 512;       
        private const int JOB_STATUS_DELETED = 256;            
        private const int JOB_STATUS_PRINTED = 128;            
        private const int JOB_STATUS_PAPEROUT = 64;                            
        private const int JOB_STATUS_OFFLINE = 32;             
        private const int JOB_STATUS_PRINTING = 16;     
        private const int JOB_STATUS_SPOOLING = 8;      
        private const int JOB_STATUS_DELETING = 4;                       
        private const int JOB_STATUS_ERROR = 2;                
        private const int JOB_STATUS_PAUSED = 1;
                                                                       
        private Dictionary<int, string> jDict = new Dictionary<int, string>()
            {
                {JOB_STATUS_RETAINED, "Retained"},
                {JOB_STATUS_COMPLETE, "Complete"},
                {JOB_STATUS_RESTART, "restarted"},
                {JOB_STATUS_USER_INTERVENTION, "UserIntervention"},
                {JOB_STATUS_BLOCKED_DEVQ, "Blocked"},
                {JOB_STATUS_DELETED, "Deleted"},
                {JOB_STATUS_PRINTED, "Printed"},
                {JOB_STATUS_PAPEROUT, "PaperOut"},
                {JOB_STATUS_OFFLINE, "Offline"},
                {JOB_STATUS_PRINTING, "Printing"},
                {JOB_STATUS_SPOOLING, "Spooling"},
                {JOB_STATUS_DELETING, "Deleting"},
                {JOB_STATUS_ERROR, "Error"},
                {JOB_STATUS_PAUSED, "Paused"},                      
            };
        private StringBuilder Get_Status(JOB_INFO_2W[] jobInfos, /*... your parameters, if any*/)
        {
            string jobStatus;
            StringBuilder sb;
            foreach (var job in jobInfos)
            {
                jobStatus = "";
                foreach (var item in jdict)
                {
                    if ((job.Status & item.Key) == item.Key)
                    {
                        jobStatus = item.Value;
                        break; // or just ignore this line if you look for all combined statuses
                    }
                }
                sb.Append("Status = " + jobStatus);
            }
            return sb;
        }
    }
}