【发布时间】:2014-11-07 04:07:56
【问题描述】:
我想在 .NET 4 中使用 C# 获取排队打印作业及其文件路径的列表。我查看了 this,但没有看到任何关于文件路径的信息。我尝试了以下代码(从某处的示例修改),希望 job.Name 或 job.JobName 是我正在寻找的,但它没有运行:
当有作业时调用GetPrintJobInfoCollection() 总是引发NullReferenceException(打印机离线,所以作业总是在队列中等待)。从队列中删除作业(通过单击右下角的打印机图标)时不会发生异常。
所以 2 个问题:
- 为什么是
NullReferenceException? - 如果此方法失败,还有其他方法可以获取排队的文件路径吗?
提前致谢!
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。刚发现是打印机离线造成的。