{"id":5648,"date":"2014-04-04T15:17:18","date_gmt":"2014-04-04T15:17:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/04\/how-to-get-printer-info-in-net-collection-of-common-programming-errors\/"},"modified":"2014-04-04T15:17:18","modified_gmt":"2014-04-04T15:17:18","slug":"how-to-get-printer-info-in-net-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/04\/how-to-get-printer-info-in-net-collection-of-common-programming-errors\/","title":{"rendered":"How to get Printer Info in .NET?-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1aa48f7606f5b08595b0a0356a61e8b6?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJohn Saunders<\/p>\n<p>In the standard PrintDialog there are four values associated with a selected printer: Status, Type, Where, and Comment.<\/p>\n<p>If I know a printer&#8217;s name, how can I get these values in C# 2.0?<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/99385f27c10871d587fd73aea958ec94?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPanos<\/p>\n<p>As dowski suggested, you could use WMI to get printer properties. The following code displays all properties for a given printer name. Among them you will find: PrinterStatus, Comment, Location, DriverName, PortName, etc.<\/p>\n<pre><code>using System.Management;\n<\/code><\/pre>\n<p>&#8230;<\/p>\n<pre><code>string printerName = \"YourPrinterName\";\nstring query = string.Format(\"SELECT * from Win32_Printer WHERE Name LIKE '%{0}'\", printerName);\nManagementObjectSearcher searcher = new ManagementObjectSearcher(query);\nManagementObjectCollection coll = searcher.Get();\n\nforeach (ManagementObject printer in coll)\n{\n    foreach (PropertyData property in printer.Properties)\n    {\n        Console.WriteLine(string.Format(\"{0}: {1}\", property.Name, property.Value));\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/3a8a3460af7d1f691872764c7295fcd7?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPowerlord<\/p>\n<p>This <em>should<\/em> work.<\/p>\n<pre><code>using System.Drawing.Printing;\n<\/code><\/pre>\n<p>&#8230;<\/p>\n<pre><code>PrinterSettings ps = new PrinterSettings();\nps.PrinterName = \"The printer name\"; \/\/ Load the appropriate printer's setting\n<\/code><\/pre>\n<p>After that, the various properties of PrinterSettings can be read.<\/p>\n<p>Note that <code>ps.isValid()<\/code> can see if the printer actually exists.<\/p>\n<p>Edit: One additional comment. Microsoft recommends you use a PrintDocument and modify its PrinterSettings rather than creating a PrinterSettings directly.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0c7e0de930a9096ad305b3289bc48de3?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nitsho<\/p>\n<p>Please notice that the article that <strong><em>dowski<\/em><\/strong> and <strong><em>Panos<\/em><\/strong> was reffering to (MSDN Win32_Printer) can be a little misleading.<\/p>\n<p>I&#8217;m referring the first value of most of the arrays. some begins with 1 and some begins with 0. for example, &#8220;<em>ExtendedPrinterStatus<\/em>&#8221; first value in table is 1, therefore, your array should be something like this:<\/p>\n<pre><code>string[] arrExtendedPrinterStatus = { \n    \"\",\"Other\", \"Unknown\", \"Idle\", \"Printing\", \"Warming Up\",\n    \"Stopped Printing\", \"Offline\", \"Paused\", \"Error\", \"Busy\",\n    \"Not Available\", \"Waiting\", \"Processing\", \"Initialization\",\n    \"Power Save\", \"Pending Deletion\", \"I\/O Active\", \"Manual Feed\"\n};\n<\/code><\/pre>\n<p>and on the other hand, &#8220;<em>ErrorState<\/em>&#8221; first value in table is 0, therefore, your array should be something like this:<\/p>\n<pre><code>string[] arrErrorState = {\n    \"Unknown\", \"Other\", \"No Error\", \"Low Paper\", \"No Paper\", \"Low Toner\",\n    \"No Toner\", \"Door Open\", \"Jammed\", \"Offline\", \"Service Requested\",\n    \"Output Bin Full\"\n};\n<\/code><\/pre>\n<p>BTW, &#8220;<em>PrinterState<\/em>&#8221; is obsolete, but you can use &#8220;<em>PrinterStatus<\/em>&#8220;.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8fcf279def5c1abd4cdf30b974dd8787?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDavid<\/p>\n<p>Just for reference, here is a list of all the available properties for a printer ManagementObject.<\/p>\n<pre><code>usage: printer.Properties[\"PropName\"].Value\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b778d6629c683e80b52c5de444414fa6?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\ndowski<\/p>\n<p>It&#8217;s been a long time since I&#8217;ve worked in a Windows environment, but I would suggest that you look at using WMI.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/743f83ddfb938455547baf45849d6fb3?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMarkMiddlemist<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>John Saunders In the standard PrintDialog there are four values associated with a selected printer: Status, Type, Where, and Comment. If I know a printer&#8217;s name, how can I get these values in C# 2.0? Panos As dowski suggested, you could use WMI to get printer properties. The following code displays all properties for a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5648","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5648"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5648\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}