我正在为WPF中的窗口服务创建一个设置。该服务始终在我的系统中运行。我想得到打印页数。如果我要从我的计算机上打印任何文档,如doc,PDF,excel,该服务将自动计算打印页数和当前日期时间。然后它将为它创建日志。如何在WPF中使用C#编码获取打印页数。
答案 0 :(得分:2)
这可能会帮助你获得打印的数量
try
{
string PrinterJobs = "SELECT * FROM Win32_PrintJob";
ManagementObjectSearcher FindPrintJobs = new ManagementObjectSearcher(PrinterJobs);
ManagementObjectCollection prntJobCollection = FindPrintJobs.Get();
foreach (ManagementObject prntJob in prntJobCollection)
{
string jobName = prntJob.Properties["Name"].Value.ToString();
string documentName = prntJob.Properties["Document"].Value.ToString();
string nbcopie = prntJob.Properties["TotalPages"].Value.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
您还可以查看PrintMonitor – A C# print spooler monitor
修改强>
Just Applied IList<ManagementBaseObject>
获取您PC上安装的所有网络打印机的列表。正如您的评论所示。
try
{
IList<ManagementBaseObject> printers = new List<ManagementBaseObject>();
string PrinterJobs = "SELECT * FROM Win32_PrintJob";
ManagementObjectSearcher FindPrintJobs = new ManagementObjectSearcher(PrinterJobs);
ManagementObjectCollection prntJobCollection = FindPrintJobs.Get();
foreach (ManagementObject prntJob in prntJobCollection)
{
if ((bool)printer["Network"])
{
printers.Add(printer);
}
string jobName = prntJob.Properties["Name"].Value.ToString();
string documentName = prntJob.Properties["Document"].Value.ToString();
string nbcopie = prntJob.Properties["TotalPages"].Value.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}