问题
I've created an application in .net to monitor jobs in the printer by using the DLL in the following reference :
http://www.codeproject.com/Articles/51085/Monitor-jobs-in-a-printer-queue-NET?fid=1556859&select=4799234
my question is : how can i delay or prevent user from printing after he print for example 5 times a day ?
Knowing that The print jobs will saved in the DB.
I appreciate any help.
回答1:
A fairly simply program could be written using the FindFirstPrinterChangeNotification function that monitors all printers available to the user. It should watch for the PRINTER_CHANGE_ADD_JOB
event, which indicates a print job has been started. Once the user has exceeded the criteria, your program would then delete any new print jobs the user started. I think it would be best to notify the user rather than just deleting their print jobs silently.
This doesn't prevent users from trying to print, but it does have the same end result. No paper will be produced until your application allows it.
回答2:
Solved ! I've used the following methods to solve my problem :
to pause printer Job call this :
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
And to cancel printer jobs i've used the following method :
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
And if need to resume the printing after pause then you should use :
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
来源:https://stackoverflow.com/questions/23084052/prevent-user-from-printing