469,317 Members | 1,739 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

Im using win32_printer i show the user the printer properties but it show me twice:

Im using win32_printer and im adding to listbox1 a list of the printer properties. but then when im scrolling down in the listbox1 and getting to the last property i see the list of properties start over again from the begining.

So its adding twice the list of properties of the printer to the listbox1 and i want to add it once.

Now i found that when im adding first the list of printers connected to the pc to combobox1 i see like 3-4 printer devices connected even if only one is connected physicaly:

My printer is: HP Officejet 6550 E USB Device.
So i can try to remove the other printer devices manualy and leave only one through the ControlPanel but its not a solution. Since my program is working with usb inseretion/removal events i dont want the user each time he inesrt a printer he will have to remove some devices of it manualy.

So my first problem is how to make that it will show me only once the properties list in the listbox1?

Here is the code im using in the contructor in my project:

I must say also if im doing it with Win32_DiskDrive for example i see the properties only once on any hard disk i have in the pc or any hard disk i have external on usb.

Only on Win32_Printer i have this problem.

The code:

Expand|Select|Wrap|Line Numbers
  1.  private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.  
  4.             ManagementObjectSearcher mosPrinter = new ManagementObjectSearcher( "SELECT * FROM Win32_Printer");
  5.             foreach (ManagementObject moDprinte in mosPrinter.Get())
  6.             {
  7.  
  8.                 printerName = moDprinte["Name"].ToString().ToLower();
  9.                 if (printerName.Equals(@"hp officejet"))
  10.                 {
  11.  
  12.                     comboBox1.Items.Add(moDprinte["Name"].ToString());
  13.  
  14.                 }
  15.                 foreach (PropertyData p in moDprinte.Properties)
  16.                 {
  17.  
  18.                     listBox1.Items.Add(String.Format("{0}: {1}", p.Name, p.Value == null ? "NULL" : p.Value));
  19.                 }
  20.             }
  21.  
Sep 7 '10 #1
3 3332
After some breakpoints and tries...i found that Win32_DiskDrive act and detect like Win32_Printer.

Here is the code untill the constructor where the problem is:


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Media;
  10. using System.Resources;
  11. using System.Management;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Reflection;
  15. using System.Management.Instrumentation;
  16. using System.Drawing.Printing;
  17.  
  18. namespace Hard_Disk_Info
  19. {
  20.     public partial class Form1 : Form
  21.     {
  22.         string printerName = "";
  23.         PrintPreview myPrintpreview;
  24.         string stringToPrint;                          
  25.         Options myOptions;
  26.         ManagementEventWatcher watcher;
  27.         ManagementEventWatcher watchit;
  28.         ManagementScope scope;
  29.         bool Check_For_Closing;
  30.         ManagementObjectSearcher objSearcher;
  31.         System.Management.ManagementObjectCollection objColl;
  32.  
  33.         public Form1()
  34.         {
  35.             InitializeComponent();
  36.             printDocument1.PrintPage +=
  37.                 new PrintPageEventHandler(printDocument1_PrintPage);
  38.             this.Width = 844;
  39.             myOptions = new Options();
  40.             objSearcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
  41.             scope = new ManagementScope("root\\CIMV2");
  42.             scope.Options.EnablePrivileges = true;
  43.  
  44.             this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
  45.  
  46.             try
  47.             {
  48.                 WqlEventQuery query = new WqlEventQuery();
  49.                 query.EventClassName = "__InstanceCreationEvent";
  50.                 query.WithinInterval = new TimeSpan(0, 0, 1);
  51.                 query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
  52.  
  53.                 watcher = new ManagementEventWatcher(scope, query);
  54.                 watcher.EventArrived += new EventArrivedEventHandler(WaitForUSBChangeEvent);
  55.                 watcher.Start();
  56.  
  57.             }
  58.             catch (ManagementException)
  59.             {
  60.             }
  61.  
  62.             try
  63.             {
  64.                 WqlEventQuery query = new WqlEventQuery();
  65.                 query.EventClassName = "__InstanceDeletionEvent";
  66.                 query.WithinInterval = new TimeSpan(0, 0, 1);
  67.                 query.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
  68.  
  69.                 watchit = new ManagementEventWatcher(scope, query);
  70.                 watchit.EventArrived += new EventArrivedEventHandler(WaitForUSBRemovalEvent);
  71.                 watchit.Start();
  72.             }
  73.             catch
  74.             {
  75.             }
  76.  
  77.  
  78.             Check_For_Closing = false;
  79.             objColl = objSearcher.Get();
  80.             comboBox1.Items.Add("REFRESH THE DEVICES LIST");
  81.             comboBox1.Items.Add("SYSTEM INFORMATION");
  82.             comboBox1.Text = "Please Make Your Selection";
  83.         }
  84.  
  85.         private void Form1_Load(object sender, EventArgs e)
  86.         {
  87.  
  88.             ManagementObjectSearcher mosPrinter = new ManagementObjectSearcher( "SELECT * FROM Win32_Printer");
  89.             foreach (ManagementObject moDprinte in mosPrinter.Get())
  90.             {
  91.  
  92.             //    printerName = moDprinte["Name"].ToString().ToLower();
  93.              //   if (printerName.Equals(@"hp officejet"))
  94.               //  {
  95.  
  96.                //     comboBox1.Items.Add(moDprinte["Name"].ToString());
  97.  
  98.               //  }
  99.  
  100.                 foreach (PropertyData p in moDprinte.Properties)
  101.                 {
  102.  
  103.                     listBox1.Items.Add(String.Format("{0}: {1}", p.Name, p.Value == null ? "NULL" : p.Value));
  104.                 }
  105.  
  106.  
  107.               //  listBox1.Items.Add("hello");
  108.              }
  109.             ManagementObjectSearcher cdDrive = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
  110.             // Loop through each object (disk) retrieved by WMI
  111.  
  112.             foreach (ManagementObject moDiskete in cdDrive.Get())
  113.             {
  114.                 if (comboBox1.Items.Contains(moDiskete["Name"]) == false)
  115.                 {
  116.                     // Add the HDD to the list (use the Model field as the item's caption)
  117.                     comboBox1.Items.Add(moDiskete["Name"].ToString());
  118.                 }
  119.  
  120.             }
  121.             ManagementObjectSearcher mosDiskst = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
  122.             // Loop through each object (disk) retrieved by WMI
  123.  
  124.             foreach (ManagementObject moDiskete in mosDiskst.Get())
  125.             {
  126.                 if (comboBox1.Items.Contains(moDiskete["Model"]) == false)
  127.                 {
  128.                     // Add the HDD to the list (use the Model field as the item's caption)
  129.                     comboBox1.Items.Add(moDiskete["Model"].ToString());
  130.                 }
  131.  
  132.             }
  133.  

Now the problem:



In the end of this code i just pasted im adding to the combobox1 from Win32_DiskDrive the list of models of disk drives:

Expand|Select|Wrap|Line Numbers
  1. comboBox1.Items.Add(moDiskete["Model"].ToString());
  2.  

Now when i run my application and check the combobox1 i see list of all my hard disks and in the end i see the printer. But the printer is also a disk drive? Strange.

cuz i dont add the printer in any place in this code to the combobx1 so how come its listed there?

I found that if i remove the line: comboBox1.Items.Add(moDiskete["Model"].ToString());


So i dont see any hard disk in the list but also i dont see any printer.

Now above it in the Win32_Printer loop im trying to adding the printer properties to listbox1.

But its adding the list of the properties twice instead once.

I think that since Win32_DiskDrive detect the printer as disk drive so when its adding the properties to the listbox1 its adding once as Win32_Printer and second time as Win32_DiskDrive.



So now i think i know the problem.

But whats the solution? How can i make it to see the diffrenet between Win32_Printer and Win32_DiskDrive so Win32_Printer will detect only printers and Win32_DiskDrive will detect only hard disks.

Maybe something is worng and i dont understand but from what i tried so far it seems that this is the problem.



Any ideas how to solve it?



Thanks for helping.
Sep 7 '10 #2
Now i tried another thing its getting more harder to understand the problem:



This is the code of the Win32_Printer in the constructor after i added a line:



Expand|Select|Wrap|Line Numbers
  1. ManagementObjectSearcher mosPrinter = new ManagementObjectSearcher( "SELECT * FROM Win32_Printer");
  2.             foreach (ManagementObject moDprinte in mosPrinter.Get())
  3.             {
  4.  
  5.             //    printerName = moDprinte["Name"].ToString().ToLower();
  6.               //  if (printerName.Equals(@"hp officejet"))
  7.                 //{
  8.  
  9.                     comboBox1.Items.Add(moDprinte["Name"].ToString());
  10.  
  11.                // }
  12.  
  13.                 foreach (PropertyData p in moDprinte.Properties)
  14.                 {
  15.  
  16.                     listBox1.Items.Add(String.Format("{0}: {1}", p.Name, p.Value == null ? "NULL" : p.Value));
  17.                 }
  18.  
  19.  
  20.  
  21.              }
  22.  
The kine i added here is: comboBox1.Items.Add(moDprinte["Name"].ToString());


And now when i run my application and look in the devices list i see:

HP Officejet 6500 E709a Series (Copy 1)

HP Officejet 6500 E709a Series

And in the bottom i see:HP Officejet 6500 E USB

Now the last one Hp Officejet 6500 E USB is the one that the Win32_DiskDrive detecting as i said above.

The other two its the the Win32_Printer that detecting.

If i removeo ne of the line for example if i remove comboBox1.Items.Add(moDprinte["Name"].ToString()); from the Win32_Printer code area

ill see in the combBox1 only HP Officejet 6500 E USB.

If i also remove the line: comboBox1.Items.Add(moDiskete["Model"].ToString()); from the Win32_DiskDrive i will not see any hard disks in the combobx1 and

no printers.

So i must keep this line comboBox1.Items.Add(moDiskete["Model"].ToString()); in the Win32_DiskDrive to see wich hard disks there are.

But again this is strange cuz the Win32_DiskDrive detect the printer as a usb diskdrive maybe cuz the printer is connected through usb.

And therefore i think in my Win32_Printer when i try to add anything to the listbox1 even just simple text like "hello" its adding it twice.

Any ideas how to solve it? Why if there is a Win32_Printer then also Win32_DiskDrive detect the printer?



Thanks for helping.
Sep 8 '10 #3
Plater
7,872 Expert 4TB
In your original code you were adding the properties of every printer on your system to the listbox.
Sep 9 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Eagle | last post: by
11 posts views Thread by Rimpinths | last post: by
2 posts views Thread by Morten Fagermoen | last post: by
2 posts views Thread by =?Utf-8?B?QW1pciBUb2hpZGk=?= | last post: by
3 posts views Thread by =?Utf-8?B?QXho?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.