BLOG.CSHARPHELPER.COM: Use WMI to get detailed information about printers in C#
Use WMI to get detailed information about printers in C#
When the program loads, it loops through the PrinterSettings.InstalledPrinters collection to builds a list of the system's defined printers.
// List the installed printers. private void Form1_Load(object sender, EventArgs e) { // Find all of the installed printers. foreach (string printer in PrinterSettings.InstalledPrinters) { cboPrinters.Items.Add(printer); }
// Select the first printer. cboPrinters.SelectedIndex = 0; }
When you select a printer from the list, the program uses Windows Management Instrumentation (WMI) to get information about that printer. It displays the printer's name, state, status, description, default-ness, vertical and horizontal resolution, port name, and paper sizes supported.
(WMI is a sort of SQL-like scripting language that lets you learn ab out and control many parts of the system that are hard to get at through the .NET Framework or even API functions. It's pretty amazing what kinds of information are in there if you can figure out how to find it. Note that to use WMI you need to add a reference to System.Management.)
The following code shows how the program responds when you select a printer.
// Get a ManagementObjectSearcher for the printer. string query = "SELECT * FROM Win32_Printer WHERE Name='" + cboPrinters.SelectedItem.ToString() + "'"; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
// Get the ManagementObjectCollection representing // the result of the WMI query. Loop through its // single item. Display some of that item's properties. foreach (ManagementObject service in searcher.Get()) { txtName.Text = service.Properties["Name"].Value.ToString();
UInt32 state = (UInt32)service.Properties["PrinterState"].Value; txtState.Text = PrinterStates[state];
UInt16 status = (UInt16)service.Properties["PrinterStatus"].Value; txtStatus.Text = PrinterStatuses[status];
// List the available properties. foreach (PropertyData data in service.Properties) { string txt = data.Name; if (data.Value != null) txt += ": " + data.Value.ToString(); Console.WriteLine(txt); } } }
Some of the values returned by the WMI printer structure are numeric so the function builds some arrays of strings to convert them into textual values.
The code then executes the statement SELECT * FROM Win32_Printer WHERE Name='name' for the printer you selected. The result is a ManagementObjectCollection containing a list of ManagementObjects. This example selects information for a single printer so the collection only holds one object.
The code examines the object and uses its Properties collection to retrieve key values and displays them. To make this a bit easier, the code uses the GetPropertyValue function described shortly.
The ManagementObject's PrinterPaperNames property contains an array of paper size names. The code loops through this array adding the paper sizes to a listbox.
Finally so you can see what values are available, the program loops through all of the object's properties writing their names to the Console window.
The GetPropertyValue function gets a property value from a PropertyData object. If the object or its Value property is null, the function returns an empty string. Otherwise it returns the object's Value converted into a string.
// If the data is not null and has a value, return it. private string GetPropertyValue(PropertyData data) { if ((data == null) || (data.Value == null)) return ""; return data.Value.ToString(); }
6/16/2010 5:30 AM
Printer Cartridges wrote:
Oooh nice! Thanks for that code! I've been looking for a way to get info on my printers, and I wasn't sure how to go about it. You've cleared this up for me, thanks so much :) Reply to this
11/12/2010 2:26 AMasos voucher wrote:
Thanks for giving me the information about C# that how can I use printers in C#. Now I will use the printers in C# with the help of Windows Management Instrumentation (WMI). Reply to this
1/7/2011 4:23 PMRod Stephens wrote:
I don't think this example had an image, but there's no reason why it shouldn't so I added one.
I build these pages in Firefox using the GoDaddy Quick Blogcast tool, so I can't vouch for the pages displaying perfectly on all browsers. Reply to this
Oooh nice! Thanks for that code! I've been looking for a way to get info on my printers, and I wasn't sure how to go about it. You've cleared this up for me, thanks so much :)
Reply to this
Thanks for giving me the information about C# that how can I use printers in C#. Now I will use the printers in C# with the help of Windows Management Instrumentation (WMI).
Reply to this
Trying to see this image. Your layout isn't working correctly with safari browser on imac.
Reply to this
I don't think this example had an image, but there's no reason why it shouldn't so I added one.
I build these pages in Firefox using the GoDaddy Quick Blogcast tool, so I can't vouch for the pages displaying perfectly on all browsers.
Reply to this