473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3828
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

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

Similar topics

2
by: Eagle | last post by:
Is there any way to make a print button on a form show the printer properties dialog before actual printing the report linked to the command button in AC97 ? I've noticed that opening the report...
9
by: PJ6 | last post by:
I'm sure it's some attribute I have to set but I can't find it. I'd like to be able to have my properties in my custom web user controls show up in the web forms designer. Is that possible? Paul
0
by: mac8788 | last post by:
I am writing my first VB.NET program that needs to manipulate printers. Explicitly, I need to be able to set the paper source (tray) of a known installed printer. I've looked at...
27
by: Just Me | last post by:
I made a Usercontrol that must have AutoScroll set to true when it is used. So I set it to True in the Load event. However the property still shows in the properties window when the control is...
0
by: ripal.soni | last post by:
Hi there I am .net developer ,right now working on a project in that i need to print pdf file using vb.net and i also have to pass printer name as argument ,file should be printed on a...
11
by: Rimpinths | last post by:
I'm new at developing user controls in C#, and one thing I've noticed right off the bat is that the constructor gets called twice -- once at design time, once at run time. In short, I'm trying...
2
by: Morten Fagermoen | last post by:
Hi! How can I get the printer properties (the same as right click-properties in the Printer menu) in a VB.NET 2005 application. I have the collection of the printers but need to get to the...
2
by: =?Utf-8?B?QW1pciBUb2hpZGk=?= | last post by:
Hi I have created a user control which has a public property called Tickers. This property is represented visually as a textbox into which the user can type a bunch of stock tickers and my user...
3
by: =?Utf-8?B?QXho?= | last post by:
Is there a limitation on the number of (sequential) opened connection for SQL Server User Instances? The following method will run to about the 240th iteration then will receive a timeout...
2
by: srilakshmi15 | last post by:
programmatically i should be able to get all the properties of the printer when i use this link http://www.codeproject.com/KB/dotnet/NET_Printer_Library.aspx i can only get some of the common...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.