Elencare i print job tramite WMI

Uno snippet per i print job in corso.

Dim path As New System.Management.ManagementPath(“root\cimv2”)
Dim options As New System.Management.ConnectionOptions

‘Local Host
path.Server = “.”

‘Remote Host
‘path.Server = “ServerName”
‘options.Username = “DomainName\UserName”
‘options.Password = “UserPassword”

Dim scope As New System.Management.ManagementScope(path, options)
Dim query As New System.Management.ObjectQuery( _
       “SELECT Name, Document, JobStatus, Owner, TotalPages, Size, TimeSubmitted FROM Win32_PrintJob”)
Dim search As New System.Management.ManagementObjectSearcher(scope, query)
Dim objectCollection As System.Management.ManagementObjectCollection

objectCollection = search.Get()

Dim printerName, document, owner As String
Dim pages, size As System.UInt32
Dim timeSubmitted As DateTime
For Each prntJob As System.Management.ManagementObject In objectCollection
       printerName = prntJob(“Name”).ToString().Split(“,”c)(0)
       document = prntJob(“Document”).ToString()
       owner = prntJob(“Owner”).ToString()
       pages = System.Convert.ToUInt32(prntJob(“TotalPages”))
       size = System.Convert.ToUInt32(prntJob(“Size”))
       timeSubmitted = System.Management.ManagementDateTimeConverter.ToDateTime( _
                prntJob(“TimeSubmitted”).ToString())

       Console.WriteLine(“Printer: ” & printerName)
       Console.WriteLine(“Document: ” & document)
       Console.WriteLine(“Owner: ” & owner)
       Console.WriteLine(“Pages: ” & IIf(pages = 0, “N/D”, pages.ToString(“#,##0”)).ToString())
       Console.WriteLine(“Size: ” & System.Convert.ToInt32(size / 1024).ToString(“#,##0″) & ” KB”)
       Console.WriteLine(“Submitted: ” & timeSubmitted.ToString(“G”))

       Console.WriteLine(New String(“-“c, 50))
Next

Per approfondimenti si vedano i seguenti link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_printjob.asp
http://support.microsoft.com/kb/836877/en-us