2014年6月5日 星期四

vbnet 檢查印表機是否 online

--是否有安裝

Imports System.Drawing.Printing
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(CheckPrinter("Microsoft XPS Document Writer").ToString)
        MessageBox.Show(CheckPrinter("EPSON Stylus Photo 895").ToString)
    End Sub
    Private Function CheckPrinter(ByVal printerName As String) As Boolean
        Dim online As Boolean = False
        Try
            Dim mprintDocument As PrintDocument = New PrintDocument()
            mprintDocument.PrinterSettings.PrinterName = printerName
            online = mprintDocument.PrinterSettings.IsValid
        Catch
            online = False
        End Try
        Return online
    End Function
End Class

---狀態判斷
 Private Enum PrinterStatus
PrinterIdle = 3
PrinterPrinting = 4
PrinterWarmingUp = 5
' For more states see WMI docs.
End Enum

Private Function PrinterStatusToString(ByVal ps As PrinterStatus) As
String
Dim s As String
Select Case ps
Case PrinterStatus.PrinterIdle
s = "waiting (idle)"
Case PrinterStatus.PrinterPrinting
s = "printing"
Case PrinterStatus.PrinterWarmingUp
s = "warming up"
Case Else ' Vielleicht gibt es noch weitere Falle...
s = "unknown state"
End Select
PrinterStatusToString = s
End Function

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
Dim strPrintServer As String
strPrintServer = "localhost"
Dim WMIObject As String, PrinterSet As Object, Printer As Object
WMIObject = "winmgmts://" & strPrintServer
PrinterSet = GetObject(WMIObject).InstancesOf("win32_Printer")
For Each Printer In PrinterSet
MsgBox( _
Printer.Name & ": " & _
PrinterStatusToString(Printer.PrinterStatus) _
)
Next Printer
End Sub

沒有留言:

張貼留言