locked
filter printers based on model RRS feed

  • Question

  • I am using VS 2008, I have this code to get the printer list in to combo box. But if i need to filter the based on the model , how do i filter.

     

    PrintDocument prtdoc = new PrintDocument();
                string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;
                foreach (String strPrinter in PrinterSettings.InstalledPrinters)
                {
                    comboBox1.Items.Add(strPrinter);
                    if(strPrinter == strDefaultPrinter)
                    {
                        comboBox1.SelectedIndex = comboBox1.Items.IndexOf(strPrinter);
                    }
                }

     

    Any help will be appreciated.

    Sunday, May 23, 2010 4:24 PM

All replies

  • can somebody reply?
    Tuesday, May 25, 2010 3:51 PM
  • Hi,

    Could you please be more clear about the modal?

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, May 27, 2010 8:14 AM
  • I tried this real quick and dirt, so I help it sends you on the right track.

    Using this code

    using System.Management;
    namespace WpfApplication4
    {
      /// <summary>
      /// Interaction logic for Window1.xaml
      /// </summary>
      public partial class Window1 : Window
      {
        public Window1()
        {
          InitializeComponent();
        }
    
        private void Button1Click(object sender, RoutedEventArgs e)
        {
          comboBox1.Items.Clear();
    // Use the ObjectQuery to get the list of configured printers
          ObjectQuery oquery = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
     
          ManagementObjectSearcher mosearcher = new System.Management.ManagementObjectSearcher(oquery);
     
          ManagementObjectCollection moc = mosearcher.Get();
     
          foreach (ManagementObject mo in moc)
          {
            PropertyDataCollection pdc = mo.Properties;
            foreach (PropertyData pd in pdc)
            {
              if ((bool)mo["Network"])
              {
                comboBox1.Items.Add(mo[pd.Name]);
              }
            }
          }
     
       
     
          
          
        }
      }
    }

    I ran this locally and looped through my watch window to see what properties are available. When I look at the PropertyData values I see a lot of good info about my printers, but the only thing I could ascertain about the model was in the DriverName. If you have a Comment or Description on your existing printer installs, you could find those out as well.

    For instance, I loop through all the installed network printers and I watched the value of the ManagementObject[PropertyData.Name] and I see the one printer is a Xerox.

    pd.Name "DriverName" string

    mo[pd.Name] "Xerox Global Print Driver PS" object {string}

    So to ignore Xerox printers I do this:

    private void Button1Click(object sender, RoutedEventArgs e)
        {
          comboBox1.Items.Clear();
          // Use the ObjectQuery to get the list of configured printers
          ObjectQuery oquery = new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
     
          ManagementObjectSearcher mosearcher = new System.Management.ManagementObjectSearcher(oquery);
     
          ManagementObjectCollection moc = mosearcher.Get();
     
          foreach (ManagementObject mo in moc)
          {
                if (!mo["DriverName"].ToString().Contains("Xerox"))
                {
                  comboBox1.Items.Add(mo["DriverName"]);
                }
          }
        }

    Be sure to add the reference to System.Management. I found the code here: http://www.dotnetcurry.com/ShowArticle.aspx?ID=148

    Thursday, May 27, 2010 5:51 PM