我想在 .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));
}
}
}
最佳答案
事实证明,只有当打印机处于脱机状态时才会出现异常。当打印机在线时,job.Name
给出文件路径,而 job.JobName
只说“打印系统文档”。
关于c# - 如何在 C# 中获取队列中打印作业的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26793885/