C# to find JobId, Owner, TotalPages from Printer

Posted by tanthiamhuat on Stack Overflow See other posts from Stack Overflow or by tanthiamhuat
Published on 2010-05-12T08:51:25Z Indexed on 2010/05/12 8:54 UTC
Read the original article Hit count: 574

Filed under:

My below code works when the Form loads and I can get a list of Network Printers in listBox1. I am also able to see a specific printer's property name and value in listBox2.

private void Form1_Load(object sender, EventArgs e) { foreach (String printer in PrinterSettings.InstalledPrinters) {

listBox1.Items.Add(printer.ToString()); } }

private void button1_Click(object sender, EventArgs e) { string printerName = "Ricoh-L4-1"; 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) { listBox2.Items.Add(string.Format("{0}: {1}", property.Name, property.Value)); } } }

But when I try to find JobId, Owner, TotalPages for a particular printer (when I actually send jobs to print to that printer), those values do not display at all.

private void button2_Click(object sender, EventArgs e) { listBox2.Items.Clear(); string printerName = "Ricoh-L4-1"; string query = string.Format("SELECT * from Win32_PrintJob WHERE Name LIKE '%{0}'", printerName);

ManagementObjectSearcher SearchPrintJobs = new ManagementObjectSearcher(query); ManagementObjectCollection PrntJobCollection = SearchPrintJobs.Get(); foreach (ManagementObject PrntJob in PrntJobCollection) { string m_JobID = PrntJob.Properties["JobId"].Value.ToString(); string m_Owner = PrntJob.Properties["Owner"].Value.ToString(); string m_TotalPages = PrntJob.Properties["TotalPages"].Value.ToString(); listBox2.Items.Add(string.Format("{0}:{1}:{2}", m_JobID,m_Owner,m_TotalPages)); } }

Do you have any idea how to get above to work? thanks in advance.

© Stack Overflow or respective owner

Related posts about printer