Discussion:
Selecting printers using WMI
(too old to reply)
Paul S
2007-03-29 09:24:01 UTC
Permalink
When I select printers may application sometimes hangs if the customer has
many printers installed.

This is how I do it:

EnumerationOptions options = new EnumerationOptions();
options.ReturnImmediately = true;
options.Rewindable = false;

// Select Printers from WMI Object Collections
string sql = "select DriverName, Name, Attributes, PortName, Location,
ServerName, ShareName from win32_printer";
// Select Printers from WMI Object Collections
using (ManagementObjectSearcher searcher = new
ManagementObjectSearcher(@"\root\cimv2", sql, options))
{
foreach (ManagementObject printer in searcher.Get())
....

Can it be done better -

in some cases the printers can be limited to only those that start with ex.
'my'
I have tried to write the select for that but it doesn't return any printers
- what should the select statement look like ?.
--
Paul S
Timmy
2007-05-24 06:28:02 UTC
Permalink
Paul, I thought I would add to your query as I am experiencing what appears
to be the same issue. Lots of printers cause my WMI query to go off into
never never never land for a manner of seconds (30 odd seconds) when
iterating through the management objects.

I have tried playing with the EnumerationOptions, however they seem to make
little differance and I must confess I have not had enough exposure to the
options to understand what effect they may have.

In the case of my code each loop which moves on to the next mgmtobject
happens quickly untill a certain iteration and then 30 seconds of delay. Then
the code moves through additional iteratons fine again. here is my code:
(please forgive periphal msgbox's and extraneous code). The freeze is always
on the "For Each MgmtObject In MgmtCollection" after executing the next
statement.


Private Function getPrinters(ByVal recordexist As Boolean) As Boolean
If debug Then MsgBox("Getting List of Printers from WMI")

'Declare WMI Variables
'Dim ManagementObject

Try

Dim MgmtObject As ManagementObject
Dim MgmtCollection As ManagementObjectCollection
Dim MgmtSearcher As ManagementObjectSearcher
Dim ReturnBoolean As Boolean = False

'Perform the search for printers and return the listing as a
collection
'MgmtSearcher = New ManagementObjectSearcher("Select name,
PortName from Win32_Printer")
Dim options As EnumerationOptions = New EnumerationOptions
options.ReturnImmediately = True
options.Rewindable = False
options.EnsureLocatable = True
options.EnumerateDeep = False
MgmtSearcher = New ManagementObjectSearcher("\root\cimv2",
"Select name, PrinterStatus, PortName from Win32_Printer", options)

MgmtCollection = MgmtSearcher.Get

'Enumerate Objects To Find Printer
For Each MgmtObject In MgmtCollection

If debug Then MsgBox("MgmtOptject for: " &
MgmtObject.Item("Name").ToString & vbCrLf & _
"Status: =" & MgmtObject.Item("PrinterStatus").ToString)
'Look for a match
'If InStr("MgmtOptject for: " &
MgmtObject.Item("Name").ToString, "in session") > 0 Then

'End If
If Mid(MgmtObject.Item("PortName").ToString, 1, 2) = "TS" Then
If debug Then MsgBox("Found TS Printer: " &
MgmtObject.Item("PortName").ToString)
If InStr(MgmtObject.Item("name").ToString, "in session "
& strid) Then
If debug Then MsgBox("Found Correct TS Printer for
this session: " & MgmtObject.Item("name").ToString)

tsPrinters.Items.Add(MgmtObject.Item("name").ToString)
tsPrinters.SelectedIndex = 0
HasSessionPrinters = True
End If

Else
cmbPrinterList.Items.Add(MgmtObject.Item("name").ToString)
If debug Then MsgBox("Added Printer to list: " &
MgmtObject.Item("name").ToString)
End If
If debug Then MsgBox("MgmtOptject for: " &
MgmtObject.Item("Name").ToString & " Completed, Moving on")

Next
If debug Then MsgBox("Completed List of Printers")
Catch ex As Exception
MsgBox(ex.Message)
If bhide Then ChbHide.Checked = True
If bDoNotSet Then ChBDoNotSet.Checked = True
If bRemember Then ChBRemember.Checked = True
End Try

End Function
Post by Paul S
When I select printers may application sometimes hangs if the customer has
many printers installed.
EnumerationOptions options = new EnumerationOptions();
options.ReturnImmediately = true;
options.Rewindable = false;
// Select Printers from WMI Object Collections
string sql = "select DriverName, Name, Attributes, PortName, Location,
ServerName, ShareName from win32_printer";
// Select Printers from WMI Object Collections
using (ManagementObjectSearcher searcher = new
{
foreach (ManagementObject printer in searcher.Get())
....
Can it be done better -
in some cases the printers can be limited to only those that start with ex.
'my'
I have tried to write the select for that but it doesn't return any printers
- what should the select statement look like ?.
--
Paul S
Loading...