Dictionary<string, int> GetAllPrinterQueues()
{
RaiseLog("Getting print queue counts");
Dictionary<string, int> TempDict =new Dictionary<string, int>();
PrintServer myPrintServer =new PrintServer(); // Get all the printers installed on this PC
// List the print server's queues
PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
String printQueueNames ="My Print Queues:\n\n";
foreach (PrintQueue pq in myPrintQueues)
{
Saint.StCommon.Wait((decimal)2000); // 2 seconds of "Application.DoEvents(), not thread sleep
if (GotPingBack(pq.Name))
{
int PGcount =0;
try
{
if (pq.NumberOfJobs >0)
{
// We know there are jobs. So we *have* to be able to get the collection at some point
DateTime Bailout = DateTime.Now.AddSeconds(10); // Keep trying for 10 seconds or until I get a valid response
string ErrMsg ="notyetretreived";
while (Bailout > DateTime.Now && ErrMsg !=string.Empty)
{
try
{
var Jobs = pq.GetPrintJobInfoCollection();
Saint.StCommon.Wait((int)2);
foreach (PrintSystemJobInfo Job in Jobs)
{
PGcount += Job.NumberOfPages;
ErrMsg =string.Empty;
}
}
catch (Exception k)
{
ErrMsg = k.Message;
Console.WriteLine(string.Format("{0}: {1}", pq.Name, k.Message));
}
}
}
}
catch
{
Console.WriteLine("Exception dork");
}
Console.WriteLine(string.Format("{2}\t{0}\t{1}", pq.Name, PGcount, DateTime.Now.ToString("HH:mm:ss.fff")));
TempDict.Add(pq.Name, PGcount);
}
}
return TempDict;
}
publicbool CancelPrintJob(int printJobID)
{
// Variable declarations.
bool isActionPerformed =false;
string searchQuery;
String jobName;
char[] splitArr;
int prntJobID;
ManagementObjectSearcher searchPrintJobs;
ManagementObjectCollection prntJobCollection;
try
{
// Query to get all the queued printer jobs.
searchQuery ="SELECT * FROM Win32_PrintJob";
// Create an object using the above query.
searchPrintJobs =new ManagementObjectSearcher(searchQuery);
// Fire the query to get the collection of the printer jobs.
prntJobCollection = searchPrintJobs.Get();
// Look for the job you want to delete/cancel.
foreach (ManagementObject prntJob in prntJobCollection)
{
jobName = prntJob.Properties["Name"].Value.ToString();
// Job name would be of the format [Printer name], [Job ID]
splitArr =newchar[1];
splitArr[0] = Convert.ToChar(",");
// Get the job ID.
prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);
// If the Job Id equals the input job Id, then cancel the job.
if (prntJobID == printJobID)
{
// Performs a action similar to the cancel
// operation of windows print console
prntJob.Delete();
isActionPerformed =true;
break;
}
}
return isActionPerformed;
}
catch (Exception sysException)
{
// Log the exception.
returnfalse;
}
}
补充一下:
ManagementObjectSearcher searchPrintJobs;
ManagementObjectCollection prntJobCollection;
上面这两个类在哪里声明,以及主要功能是什么。。
查询得到需要引用一个.net的装配件:System.Management.dll,这样你的项目才能使用WMI。
二、获取打印文件的队列
方法一、使用API EnumJobs
方法二、直接写job.Name
方法三、
using System.Management;
string printerName = "HP";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}%'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();
foreach (ManagementObject printer in coll)
{
foreach (PropertyData property in printer.Properties)
{
MessageBox.Show(string.Format("{0}: {1}", property.Name, property.Value));
}
}
记得Add reference, System.Mamagement这DLL
Dictionary<string, int> GetAllPrinterQueues()
{
RaiseLog("Getting print queue counts");
Dictionary<string, int> TempDict = new Dictionary<string, int>();
PrintServer myPrintServer = new PrintServer(); // Get all the printers installed on this PC
// List the print server's queues
PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
String printQueueNames = "My Print Queues:\n\n";
foreach (PrintQueue pq in myPrintQueues)
{
Saint.StCommon.Wait((decimal)2000); // 2 seconds of "Application.DoEvents(), not thread sleep
if (GotPingBack(pq.Name))
{
int PGcount = 0;
try
{
if (pq.NumberOfJobs > 0)
{
// We know there are jobs. So we *have* to be able to get the collection at some point
DateTime Bailout = DateTime.Now.AddSeconds(10); // Keep trying for 10 seconds or until I get a valid response
string ErrMsg = "notyetretreived";
while (Bailout > DateTime.Now && ErrMsg != string.Empty)
{
try
{
var Jobs = pq.GetPrintJobInfoCollection();
Saint.StCommon.Wait((int)2);
foreach (PrintSystemJobInfo Job in Jobs)
{
PGcount += Job.NumberOfPages;
ErrMsg = string.Empty;
}
}
catch (Exception k)
{
ErrMsg = k.Message;
Console.WriteLine(string.Format("{0}: {1}", pq.Name, k.Message));
}
}
}
}
catch
{
Console.WriteLine("Exception dork");
}
Console.WriteLine(string.Format("{2}\t{0}\t{1}", pq.Name, PGcount, DateTime.Now.ToString("HH:mm:ss.fff")));
TempDict.Add(pq.Name, PGcount);
}
}
return TempDict;
}
Get print job queue
public bool CancelPrintJob(int printJobID)
{
// Variable declarations.
bool isActionPerformed = false;
string searchQuery;
String jobName;
char[] splitArr;
int prntJobID;
ManagementObjectSearcher searchPrintJobs;
ManagementObjectCollection prntJobCollection;
try
{
// Query to get all the queued printer jobs.
searchQuery = "SELECT * FROM Win32_PrintJob";
// Create an object using the above query.
searchPrintJobs = new ManagementObjectSearcher(searchQuery);
// Fire the query to get the collection of the printer jobs.
prntJobCollection = searchPrintJobs.Get();
// Look for the job you want to delete/cancel.
foreach (ManagementObject prntJob in prntJobCollection)
{
jobName = prntJob.Properties["Name"].Value.ToString();
// Job name would be of the format [Printer name], [Job ID]
splitArr = new char[1];
splitArr[0] = Convert.ToChar(",");
// Get the job ID.
prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);
// If the Job Id equals the input job Id, then cancel the job.
if (prntJobID == printJobID)
{
// Performs a action similar to the cancel
// operation of windows print console
prntJob.Delete();
isActionPerformed = true;
break;
}
}
return isActionPerformed;
}
catch (Exception sysException)
{
// Log the exception.
return false;
}
}
Cancel print job
三、C#如何动态的监控打印队列的状态,包括打印开始,打印结束时给用户提示,如何获取打印份数
问:怎么用C#监控打印队列,在打印作业开始时、结束时,给用户一个提示,最后如何获得打印份数,我已经实现了获取打印页数的获取,但是无法动态的获取,望高手解答。
答:PrintDocument 的PrintPage ,BeginPrint , EndPrint事件,至于打印队列,交给系统驱动去处理,不需要自己来处理,自己写代码还未必比系统默认处理的要好,如果真的要这么做,就去找SDK上的API了。