Wednesday, May 14, 2008

How to get Printer Submitted Jobs in C# ?

Here is a handy method that gives you a collection of all the jobs submitted to the printer. This method uses the ManagementObjectSearcher and the ManagementObjectCollection classes from the .NET framework. The query “SELECT * FROM Win32_PrintJob” is used to get a collection of the submitted jobs. These jobs are added to a StringCollection and this collection is returned back.


#region GetPrintJobsCollection

/// <summary>
/// Returns the jobs in printer queue
/// </summary>
/// <param name="printerName">Printer Name.</param>
/// <returns>StringCollection</returns>
public StringCollection GetPrintJobsCollection(string printerName)
{
StringCollection printJobCollection = new StringCollection();
try
{
//Query the printer to get the files waiting to print.
string searchQuery = "SELECT * FROM Win32_PrintJob";

ManagementObjectSearcher searchPrintJobs = new ManagementObjectSearcher(searchQuery);
ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();


foreach (ManagementObject prntJob in prntJobCollection)
{
String jobName = prntJob.Properties["Name"].Value.ToString();

//Job name would be of the format [Printer name], [Job ID]
char[] splitArr = new char[1];
splitArr[0] = Convert.ToChar(",");
string prnName = jobName.Split(splitArr)[0];
string documentName = prntJob.Properties["Document"].Value.ToString();
if (String.Compare(prnName, printerName, true) == 0)
{
printJobCollection.Add(documentName);
}
}
}
catch (Exception ex)
{
// Log the exception.
}
return printJobCollection;
}

#endregion GetPrintJobsCollection

5 comments:

Unknown said...

This is quite informative.
I just need you help do you have any idea that how to check that if a file is added to the printer queue or not.

Raymond Amegadjin said...

good , i learned a lot

Unknown said...

I need a help!
I want to write a programme to access one priter by a group, while one of them doing a printing his account on printing must be decreased allowing him to print only limited number of coppies. And I want to write that in C# language.
Can anyone can guide me to the objective!!!
Thanks!!!!!!!!!!!

Unknown said...

"PagesPrinted" property of "Win32_PrintJob" class always provide 0(zero) during printing. Could you please help, how can I get the total number of printed page?

Anonymous said...

need code for old print jobs.. eg.. for the entire day

Post a Comment