[.NET] 用C#取得列表機的相關資訊

來源:互聯網
上載者:User

 

查詢列表機狀態,我利用這個查詢自己機子上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 />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.