【问题标题】:how to get filepaths of print jobs in queue in C#如何在C#中获取队列中打印作业的文件路径
【发布时间】:2014-11-07 04:07:56
【问题描述】:

我想在 .NET 4 中使用 C# 获取排队打印作业及其文件路径的列表。我查看了 this,但没有看到任何关于文件路径的信息。我尝试了以下代码(从某处的示例修改),希望 job.Namejob.JobName 是我正在寻找的,但它没有运行:

当有作业时调用GetPrintJobInfoCollection() 总是引发NullReferenceException(打印机离线,所以作业总是在队列中等待)。从队列中删除作业(通过单击右下角的打印机图标)时不会发生异常。

所以 2 个问题:

  1. 为什么是NullReferenceException
  2. 如果此方法失败,还有其他方法可以获取排队的文件路径吗?

提前致谢!

public static void testPrinter()
{
    PrintQueueCollection printQueues = null;
    PrintServer printServer = new PrintServer();
    printQueues = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });

    foreach (PrintQueue printQueue in printQueues)
    {
        foreach (var job in printQueue.GetPrintJobInfoCollection())
        {
            Console.WriteLine(string.Format("jobname={0} name={1} size={2} status={3}", job.JobName, job.Name, job.JobSize, job.JobStatus));
        }
    }
}

【问题讨论】:

  • 哪一行抛出异常?你能发布异常堆栈跟踪吗?
  • 带有printQueue.GetPrintJobInfoCollection() 的行会抛出NullReferenceException。刚发现是打印机离线造成的。

标签: c# .net printing


【解决方案1】:

事实证明,该异常仅在打印机脱机时发生。当打印机在线时,job.Name 给出文件路径,而job.JobName 只说“打印系统文档”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 2021-10-02
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多