{"id":387,"date":"2014-06-14T21:44:15","date_gmt":"2014-06-14T13:44:15","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=387"},"modified":"2018-09-04T13:27:22","modified_gmt":"2018-09-04T05:27:22","slug":"how-to-get-information-about-all-the-printers-that-are-installed-in-my-windows-machine-from-a-c-program-via-wmi","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-get-information-about-all-the-printers-that-are-installed-in-my-windows-machine-from-a-c-program-via-wmi\/","title":{"rendered":"How to get information about all the printers that are installed in my windows machine from a C# program via WMI"},"content":{"rendered":"<p>I need to create a windows service that is able to send some print jobs to one of the installed printers. As this is a windows service, it has to be able to find the printer with a preset printer name. This post details some prototyping codes that I had created to determine whether I am able to locate information of all the printers that are installed in the windows host where my windows service will run.<\/p>\n<h3>The WMI query to find all information of all the printers on my computer<\/h3>\n<p>I recalled that when I tried to <a href=\"http:\/\/www.techcoil.com\/blog\/how-to-retrieve-the-username-of-the-user-who-logged-onto-windows-from-windows-service\/\" title=\"How to retrieve the username of the user who logged onto Windows from windows service\" target=\"_blank\">find the current login user<\/a> with WMI previously, I saw the <code>Win32_Printer<\/code> class in my Windows Management Instrumentation Tester.<\/p>\n<p>I first try to see if the Win32_Printer class is able to provide me with information of the printers that are installed in my windows machine with my Windows Management Instrumentation Tester. To see all the printers that are installed in my windows machine with WMI, I connect to the <code>root\\cimv2<\/code> namespace and run the following WMI query:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nSelect * from Win32_printer \r\n<\/pre>\n<p>Indeed, with the above query, I could retrieve information of all my printers that I had installed in my windows machine.<\/p>\n<h3>The C# codes that can get information about all the printers that are installed in my windows machine<\/h3>\n<p>After verifying that the WMI query works with my Windows Management Instrumentation Tester, I proceeded to write the C# codes to retrieve information of my all printers that are installed in my windows machine:  <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nManagementScope ms = new ManagementScope(&quot;\\\\\\\\.\\\\root\\\\cimv2&quot;);\r\nObjectQuery query = new ObjectQuery(&quot;SELECT * FROM Win32_Printer&quot;);\r\nManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, query);\r\nforeach (ManagementObject mo in searcher.Get())\r\n{\r\n    Console.WriteLine(mo&#x5B;&quot;Name&quot;].ToString());\r\n}\r\n<\/pre>\n<p>I first create a <code>ManagementScope<\/code> instance that points to the <code>root\\cimv2<\/code> namespace and a <code>ObjectQuery<\/code> instance that holds the query to search for all the printers that I had installed. I then create the <code>ManagementObjectSearcher<\/code> object to perform the search. To print out the names of the printer, I iterate the <code>ManagementObject<\/code> instances found in the <code>ManagementObjectCollection<\/code> returned from a call to <code>searcher.Get()<\/code>. I then write the value of the <code>name<\/code> property of each <code>ManagementObject<\/code> instance to console.<\/p>\n<p>To get more information about the printers, we can refer to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394363(v=vs.85).aspx\" title=\"MSDN reference for the Win32_printer WMI class\" target=\"_blank\">Win32_Printer class documentation<\/a> for the available property values.<\/p>\n<h3>An additional step needed for my windows service to get information about printers installed in my windows machine.<\/h3>\n<p>After I saw my c# codes in my console application project picking up the names of the printers that are installed in my windows machine, I put them into a windows service project. However, I realized that my windows service was not able to pick up the name of any of the printers installed in my windows machine.<\/p>\n<p>I remembered that different users in windows are assigned separate sets of printer installations. As such, to ensure that my windows service sees my set of printer installation, I had to configure it to logon as my local user account via my services management console.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-6f\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-6f&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-6f&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F387&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>I need to create a windows service that is able to send some print jobs to one of the installed printers. As this is a windows service, it has to be able to find the printer with a preset printer name. This post details some prototyping codes that I had created to determine whether I am able to locate information of all the printers that are installed in the windows host where my windows service will run.<\/p>\n","protected":false},"author":1,"featured_media":1189,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[20,197,22],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/C-Sharp-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-6f","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/387"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1189"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}