Amazon Ad

Wednesday 23 December 2015

How to get printer serial number in c#

Dear All,

I was facing an issue to get Printer serial number, Here is how I was able to solve it using C#.

You can see the serial number of the model by going into "Device Manager" and then select the device and click on "Properties".
A dialog with name USB Printing Support Properties will appear, Click on the "Details" section and you will see lot of different properties of the device.
Look for the property "Device Instance Path" in case of printer. Here you will find a value like "USB\VID_XXXX&PID_XXXX\XXXXXXXXX" here "X" represents your device related numbers.

The problem was how to get these details from C#, I searched on google and found a link where I got an idea to create this utility.

Special Thanks to "Django Anonymous" for
http://stackoverflow.com/questions/20416888/to-get-provider-from-managementobjectsearcher-win32-pnpentity

The above link has a different question, But somehow I thought to use the details to build my own utility. However, I was not able to get the device name associated with the device serial number and my comboboxlist produces only serial number not the device name. I am working on it, Hopefully will give you a solution of the same.

Here is my own code in c#

    public class Device
    {
        public string DeviceName { get; set; }
        public string SerialNumber { get; set; }
    }

 public class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }

public List<Device> GetDeviceList()
{
    List<Device> devicelist = new List<Device>();
using (ManagementObjectSearcher searchernew = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption Like '%print%'"))
                {
                    foreach (ManagementObject mgmt in searchernew.Get()){
                        foreach (PropertyData property in mgmt.Properties){
                            if (property.Name == "DeviceID" && property.Value.ToString().Contains("USB\\VID_")){
                                string val = property.Value.ToString();
                                if (val.IndexOf('\\') != -1){
                                    val = val.Substring(val.LastIndexOf('\\') + 1);
                                    devicelist.Add(new Device() { SerialNumber = val, DeviceName = val });
                                }
                            }
                        }
                    }
                }
return devicelist;
}

private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                List<Device> lst = GetDeviceList();
                foreach (Device a in lst)
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = a.DeviceName;
                    item.Value = a.SerialNumber;
                    cmbPrinter.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
}


Thanks

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...