1

我在 .net 中创建了一个应用程序,通过使用以下参考中的 DLL 来监视打印机中的作业:

http://www.codeproject.com/Articles/51085/Monitor-jobs-in-a-printer-queue-NET?fid=1556859&select=4799234

我的问题是:例如每天打印 5 次后,我如何才能延迟或阻止用户打印?

知道打印作业将保存在数据库中。

我很感激任何帮助。

4

2 回答 2

0

可以使用FindFirstPrinterChangeNotification函数编写一个相当简单的程序,该函数监视用户可用的所有打印机。它应该注意PRINTER_CHANGE_ADD_JOB指示打印作业已开始的事件。一旦用户超出标准,您的程序将删除用户开始的任何新打印作业。我认为最好通知用户,而不是默默地删除他们的打印作业。

这不会阻止用户尝试打印,但它确实具有相同的最终结果。在您的申请允许之前,不会产生任何纸张。

于 2014-04-18T19:41:48.210 回答
0

解决了 !我使用以下方法来解决我的问题:

暂停打印机作业调用这个:

 Public Shared Function PausePrintJob(printerName As String, printJobID As Integer) As Boolean 
        Dim isActionPerformed As Boolean = False
        Dim searchQuery As String = "SELECT * FROM Win32_PrintJob"
        Dim searchPrintJobs As New ManagementObjectSearcher(searchQuery)
        Dim prntJobCollection As ManagementObjectCollection = searchPrintJobs.[Get]()
        For Each prntJob As ManagementObject In prntJobCollection
            Dim jobName As System.String = prntJob.Properties("Name").Value.ToString()

            Dim splitArr As Char() = New Char(0) {}
            splitArr(0) = Convert.ToChar(",")
            Dim prnterName As String = jobName.Split(splitArr)(0)
            Dim prntJobID As Integer = Convert.ToInt32(jobName.Split(splitArr)(1))
            Dim documentName As String = prntJob.Properties("Document").Value.ToString()
            If [String].Compare(prnterName, printerName, True) = 0 Then
                If prntJobID = printJobID Then
                    prntJob.InvokeMethod("Pause", Nothing)
                    isActionPerformed = True
                    Exit For
                End If
            End If
        Next
        Return isActionPerformed
    End Function

为了取消打印机作业,我使用了以下方法:

    Public Shared Function CancelPrintJob(printerName As String, printJobID As Integer) As Boolean
        Dim isActionPerformed As Boolean = False
        Dim searchQuery As String = "SELECT * FROM Win32_PrintJob"
        Dim searchPrintJobs As New ManagementObjectSearcher(searchQuery)
        Dim prntJobCollection As ManagementObjectCollection = searchPrintJobs.[Get]()
        For Each prntJob As ManagementObject In prntJobCollection
            Dim jobName As System.String = prntJob.Properties("Name").Value.ToString()

            Dim splitArr As Char() = New Char(0) {}
            splitArr(0) = Convert.ToChar(",")
            Dim prnterName As String = jobName.Split(splitArr)(0)
            Dim prntJobID As Integer = Convert.ToInt32(jobName.Split(splitArr)(1))
            Dim documentName As String = prntJob.Properties("Document").Value.ToString()
            If [String].Compare(prnterName, printerName, True) = 0 Then
                If prntJobID = printJobID Then                 
                    prntJob.Delete()
                    isActionPerformed = True
                    Exit For
                End If
            End If
        Next
        Return isActionPerformed
    End Function 



如果需要在暂停后恢复打印,那么您应该使用:

  Public Shared Function ResumePrintJob(printerName As String, printJobID As Integer) As Boolean
        Dim isActionPerformed As Boolean = False
        Dim searchQuery As String = "SELECT * FROM Win32_PrintJob"
        Dim searchPrintJobs As New ManagementObjectSearcher(searchQuery)
        Dim prntJobCollection As ManagementObjectCollection = searchPrintJobs.[Get]()
        For Each prntJob As ManagementObject In prntJobCollection
            Dim jobName As System.String = prntJob.Properties("Name").Value.ToString()
            Dim splitArr As Char() = New Char(0) {}
            splitArr(0) = Convert.ToChar(",")
            Dim prnterName As String = jobName.Split(splitArr)(0)
            Dim prntJobID As Integer = Convert.ToInt32(jobName.Split(splitArr)(1))
            Dim documentName As String = prntJob.Properties("Document").Value.ToString()
            If [String].Compare(prnterName, printerName, True) = 0 Then
                If prntJobID = printJobID Then
                    prntJob.InvokeMethod("Resume", Nothing)
                    isActionPerformed = True
                    Exit For
                End If
            End If
        Next
        Return isActionPerformed
    End Function
于 2014-05-08T08:02:52.040 回答