查詢列表機狀態,我利用這個查詢自己機子上HP列表機的狀態
string printerName = "HP";<br />string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}%'", printerName);<br />ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);<br />ManagementObjectCollection coll = searcher.Get();</p><p>foreach (ManagementObject printer in coll)<br />{<br /> foreach (PropertyData property in printer.Properties)<br /> {<br /> MessageBox.Show(string.Format("{0}: {1}", property.Name, property.Value));<br /> }<br />}
取消print job in print queue,這段代碼尋找出列表機中的print job,並刪除某一個job
public bool CancelPrintJob(int printJobID)<br />{<br /> // Variable declarations.<br /> bool isActionPerformed = false;<br /> string searchQuery;<br /> String jobName;<br /> char[] splitArr;<br /> int prntJobID;<br /> ManagementObjectSearcher searchPrintJobs;<br /> ManagementObjectCollection prntJobCollection;<br /> try<br /> {<br /> // Query to get all the queued printer jobs.<br /> searchQuery = "SELECT * FROM Win32_PrintJob";<br /> // Create an object using the above query.<br /> searchPrintJobs = new ManagementObjectSearcher(searchQuery);<br /> // Fire the query to get the collection of the printer jobs.<br /> prntJobCollection = searchPrintJobs.Get();<br /> // Look for the job you want to delete/cancel.<br /> foreach (ManagementObject prntJob in prntJobCollection)<br /> {<br /> jobName = prntJob.Properties["Name"].Value.ToString();<br /> // Job name would be of the format [Printer name], [Job ID]<br /> splitArr = new char[1];<br /> splitArr[0] = Convert.ToChar(",");<br /> // Get the job ID.<br /> prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);<br /> // If the Job Id equals the input job Id, then cancel the job.<br /> if (prntJobID == printJobID)<br /> {<br /> // Performs a action similar to the cancel<br /> // operation of windows print console<br /> prntJob.Delete();<br /> isActionPerformed = true;<br /> break;<br /> }<br /> }<br /> return isActionPerformed;<br /> }<br /> catch (Exception sysException)<br /> {<br /> // Log the exception.<br /> return false;<br /> }<br />}