我怎么知道SPL文件完全写在里面?

时间:2016-02-01 09:23:10

标签: .net printing printqueuewatch

我想获取打印机的当前打印作业信息,因此我转到文件夹C:\Windows\System32\spool\PRINTERS并使用以下代码获取假脱机文件信息,

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

但是,有时我可以正确获取打印作业颜色类型,有时则不会因为buffer = sr.ReadLine已返回NULL对象,并且我认为假脱机文件未被我的程序完全读取。

此外,我尝试了PrintQueueWatch args.PrintJob.Color,但无论我将文档颜色类型更改为“B& W”还是“Color”

,它总是返回“true”

在阅读假脱机文件或使用PrintQueueWatch时我还需要注意什么?提前谢谢〜!

1 个答案:

答案 0 :(得分:-1)

您需要监视JobWritten事件(而不是JobAdded),并且只有在触发该事件并且PrintJob.Spooling = false时才开始读取假脱机文件