阻止用户打印

本文关键字:打印 用户 | 更新日期: 2023-09-27 17:58:29

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

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

我的问题是:在用户每天打印5次之后,我如何延迟或阻止他打印

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

我感谢你的帮助。

阻止用户打印

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

这并不能阻止用户尝试打印,但它确实有相同的最终结果。在您的应用程序允许之前,不会生成任何纸张。

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

暂停打印机作业称之为:

 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