L
L1nk3R
Guest
Hello everyone,
I've already made this thread on another forum, but I decided to leave my mark here as well, since I'm not getting much help and take the opportunity to maybe reach more people... so here goes.
I'm trying to implement the functionality of monitoring the states and statuses of the currently browsable printers (local and network). I'm able to list all the printers and in correct order and I'm even able to check which printer is set to default on the computer which the application is running.
However, for a reason that is beyond my understanding, every printer that I browse has its status as "Printing" and it's state as "Paused" and it makes no sense as I even got to the point of completely shutting down a printer and the status and state remain the same...
This is the code I have for browsing all the available printers (you obviously need a combobox to populate with the printers):
Private Sub PopulatePrintersComboBox()
objMS = New System.Management.ManagementScope(ManagementPath.DefaultPath)
objMS.Connect()
objQuery = New SelectQuery("SELECT * FROM Win32_Printer")
objMOS = New ManagementObjectSearcher(objMS, objQuery)
objMOC = objMOS.Get()
Dim networkPrinterIndex As Integer = -1
For Each printer As ManagementObject In objMOC
If CBool(printer("Network")) Then
ComboBox1.Items.Add(CStr(printer("ShareName")) & " em " & CStr(printer("ServerName")).Substring(2, CStr(printer("ServerName")).Length - 2))
networkPrinterIndex += 1
If DirectCast(printer.Properties("Default").Value, Boolean) Then
ComboBox1.SelectedIndex = networkPrinterIndex
End If
End If
Next printer
For printerIndex As Integer = objMOC.Count - 1 To 0 Step -1
If CBool(objMOC(printerIndex)("Local")) Then
ComboBox1.Items.Add(objMOC(printerIndex)("Name"))
If DirectCast(objMOC(printerIndex)("Default"), Boolean) Then
ComboBox1.SelectedIndex = (objMOC.Count - 1) - printerIndex
End If
End If
Next printerIndex
End Sub
This is the code I have for checking the state and status of each selected printer:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim PrinterStatuses As String() = {"Outro", "Desconhecido", "Pronto", "A Imprimir", "A Iniciar", "Impressão Parada", _
"Offline"}
Dim PrinterStates As String() = {"Pausada", "Erro", "A Aguardar Eliminação", "Folha Encravada", "Sem Papel", "Alimentação Manual", _
"Problema de Papel", "Offline", "IO Active", "Ocupada", "Imprimindo", "Caixa de Saída Cheia", _
"Não Disponível", "Aguardando", "Processando", "Inicializando", "Iniciando", "Com Pouca Tinta", _
"Sem Tinteiro", "Page Punt", "Intervenção Manual Solicitada", "Sem Memória", "Porta Aberta", "Servidor Desconhecido", _
"Em Poupança de Energia"}
Dim state As UInt32 = DirectCast(DirectCast(objMOC(ComboBox1.SelectedIndex), _
ManagementObject).Properties("PrinterState").Value, UInt32)
lblPrinterStatus.Text = PrinterStates(state)
Dim status As UInt16 = DirectCast(DirectCast(objMOC(ComboBox1.SelectedIndex), _
ManagementObject).Properties("PrinterStatus").Value, UInt16)
lblPrinterState.Text = PrinterStatuses(status)
End Sub
Note: The states and statutes are on portuguese, so "Pausada" means "Paused" and "A Imprimir" means "Printing".
Am I doing something wrong or missing something?
Continue reading...
I've already made this thread on another forum, but I decided to leave my mark here as well, since I'm not getting much help and take the opportunity to maybe reach more people... so here goes.
I'm trying to implement the functionality of monitoring the states and statuses of the currently browsable printers (local and network). I'm able to list all the printers and in correct order and I'm even able to check which printer is set to default on the computer which the application is running.
However, for a reason that is beyond my understanding, every printer that I browse has its status as "Printing" and it's state as "Paused" and it makes no sense as I even got to the point of completely shutting down a printer and the status and state remain the same...
This is the code I have for browsing all the available printers (you obviously need a combobox to populate with the printers):
Private Sub PopulatePrintersComboBox()
objMS = New System.Management.ManagementScope(ManagementPath.DefaultPath)
objMS.Connect()
objQuery = New SelectQuery("SELECT * FROM Win32_Printer")
objMOS = New ManagementObjectSearcher(objMS, objQuery)
objMOC = objMOS.Get()
Dim networkPrinterIndex As Integer = -1
For Each printer As ManagementObject In objMOC
If CBool(printer("Network")) Then
ComboBox1.Items.Add(CStr(printer("ShareName")) & " em " & CStr(printer("ServerName")).Substring(2, CStr(printer("ServerName")).Length - 2))
networkPrinterIndex += 1
If DirectCast(printer.Properties("Default").Value, Boolean) Then
ComboBox1.SelectedIndex = networkPrinterIndex
End If
End If
Next printer
For printerIndex As Integer = objMOC.Count - 1 To 0 Step -1
If CBool(objMOC(printerIndex)("Local")) Then
ComboBox1.Items.Add(objMOC(printerIndex)("Name"))
If DirectCast(objMOC(printerIndex)("Default"), Boolean) Then
ComboBox1.SelectedIndex = (objMOC.Count - 1) - printerIndex
End If
End If
Next printerIndex
End Sub
This is the code I have for checking the state and status of each selected printer:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim PrinterStatuses As String() = {"Outro", "Desconhecido", "Pronto", "A Imprimir", "A Iniciar", "Impressão Parada", _
"Offline"}
Dim PrinterStates As String() = {"Pausada", "Erro", "A Aguardar Eliminação", "Folha Encravada", "Sem Papel", "Alimentação Manual", _
"Problema de Papel", "Offline", "IO Active", "Ocupada", "Imprimindo", "Caixa de Saída Cheia", _
"Não Disponível", "Aguardando", "Processando", "Inicializando", "Iniciando", "Com Pouca Tinta", _
"Sem Tinteiro", "Page Punt", "Intervenção Manual Solicitada", "Sem Memória", "Porta Aberta", "Servidor Desconhecido", _
"Em Poupança de Energia"}
Dim state As UInt32 = DirectCast(DirectCast(objMOC(ComboBox1.SelectedIndex), _
ManagementObject).Properties("PrinterState").Value, UInt32)
lblPrinterStatus.Text = PrinterStates(state)
Dim status As UInt16 = DirectCast(DirectCast(objMOC(ComboBox1.SelectedIndex), _
ManagementObject).Properties("PrinterStatus").Value, UInt16)
lblPrinterState.Text = PrinterStatuses(status)
End Sub
Note: The states and statutes are on portuguese, so "Pausada" means "Paused" and "A Imprimir" means "Printing".
Am I doing something wrong or missing something?
Continue reading...