-1

I'd would like to get the currently print job information of my printer, so I go to the folder C:\Windows\System32\spool\PRINTERSand using the following code to get the spool file information,

Public WithEvents _universalPrinterMonitor As PrinterQueueWatch.PrinterMonitorComponent

Private Sub UniversalPrinterMonitor_JobAdded(ByVal sender As Object, ByVal args As PrinterQueueWatch.PrintJobEventArgs) Handles _universalPrinterMonitor.JobAdded
    Try
        Dim fs As IO.FileStream
        If File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\spool\PRINTERS\" & args.PrintJob.JobId.ToString("D5") & ".SPL") Then
            fs = New IO.FileStream(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\spool\PRINTERS\" & args.PrintJob.JobId.ToString("D5") & ".SPL", _
                                   IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
            Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\spool\PRINTERS\" & args.PrintJob.JobId.ToString("D5") & ".SPL")
        ElseIf File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\spool\PRINTERS\FP" & args.PrintJob.JobId.ToString("D5") & ".SPL") Then
            fs = New IO.FileStream(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\spool\PRINTERS\FP" & args.PrintJob.JobId.ToString("D5") & ".SPL", _
                                   IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
            Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.System) & "\spool\PRINTERS\FP" & args.PrintJob.JobId.ToString("D5") & ".SPL")
        Else
            args.PrintJob.Delete()
            MessageBox.Show("There was an error occured while executing your print job, please contact your system administrator for assistance.", _
                            "Spool File Not Found!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Throw New Exception("Spool File Not Found. System Path: " & Environment.GetFolderPath(Environment.SpecialFolder.System) & ". Spool File Id: " & _
                                args.PrintJob.JobId.ToString("D5") & ".SPL")
        End If

        Using fs
            'Thread.Sleep(1000)
            Dim sr As New IO.StreamReader(fs)
            Dim buffer As String
            Dim isEOF As Boolean = False
            Dim isColor As Boolean = False

            While Not isEOF
                buffer = sr.ReadLine
                If buffer Is Nothing Then
                    isEOF = True
                    Exit While
                End If

                If buffer.Contains("RENDERMODE=COLOR") Then
                    isColor = True
                    isEOF = True
                    Exit While
                End If
            End While

However, sometime I can get the print job color type correctly, and sometime doesn't because the buffer = sr.ReadLine has returned NULL object, and I assume the spool file doesn't completely being read by my program.

Besides, I have tried the PrintQueueWatch args.PrintJob.Color, but it always return "true" no matter I change the document color type to "B&W" or "Color"

Anything else I have to notice while reading the spool file or using PrintQueueWatch? Thanks in advance~!

Steve Lam
  • 499
  • 1
  • 10
  • 27

1 Answers1

-1

You need to monitor the JobWritten event (rather than JobAdded) and only start reading a spool file if that event is triggered and PrintJob.Spooling = false

Merrion
  • 558
  • 4
  • 8