防止用户打印

时间:2014-04-15 12:41:08

标签: c# vb.net spooler print-spooler-api

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

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

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

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

我感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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