1

I have a C# application that prints simple texts correctly with printDocument Class. The problem is that I want to cancel recent print jobs or Remove them or Purge print queue, before call a new .print() function! I don't know what I have to do that, it seems PrintDocument doesn't push print jobs onto printer queue! because I dont see any job there.

thanks for help to "clear recent print jobs created with printDocumnet.print()" Ali.

Liath
  • 9,913
  • 9
  • 51
  • 81
user3224020
  • 11
  • 1
  • 3
  • I have a feeling you're unaware of the world of hurt you're entering in to when it comes to dealing with printers. If you're not able to see your print jobs in the print queue, it might be that your printer has the print-to-printer (or similarly named) option set, which will bypass your print queue all together. – Brandon Jan 22 '14 at 15:21
  • Hi, thanks. I know this future in printer option is disabled. is there any other option? I tried to work with `LocalPrintServer` And `PrintQueue` but many problems happened [see here in stack overflow](http://stackoverflow.com/questions/8348743/access-denied-trying-to-purge-printqueue-in-c-sharp) please help. – user3224020 Jan 31 '14 at 13:54

2 Answers2

1

You should be able to with WMI as long as you have the rights

http://sandit27.wordpress.com/2008/05/12/how-to-cancel-printing-in-c/

Tsukasa
  • 6,342
  • 16
  • 64
  • 96
1

I did a lot of work with printers a while back and the way that I achieved most of the functionality I wanted was through PInvoke.

As the answer from @Tsukasa states you "can" use WMI but that's another nightmare that's very difficult to deal with if you plan on actually deploying your solution anywhere other than your own machine.

As I'd mentioned: my suggestion would be to look into PInvoke. Keep in mind though that it has been a number of years/.NET Framework versions since I've worked in the print industry so Microsoft may have released a more elegant, easy-to-use solution that is native to .NET.

Here is a list of Print Spooler functions from the MSDN

Basically what you're going to want to do if you go this route is this:

  1. Find the named handle to the print device in the Registry. This can be done with the registry classes available in the .NET framework. The name of the printer to windows will start with a "#" (or two) followed by some garbled junk and then a GUID. The key where all your printers are installed in the registry is here:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers

  2. Call OpenPrinter with the printer name.

  3. Call EnumJobs with the handle retrieved from OpenPrinter.

  4. Call SetJob passing the Job Id retrieved from EnumJobs for the job you want to cancel. Be sure to pass the JOB_CONTROL_DELETE Flag to cancel the job.

  5. Call ClosePrinter, passing the handle retrieved from OpenPrinter. Don't forget this step. Windows/The print spooler gets very cranky if you do.

Again I can't stress enough that there is most-likely a better, more .NET way to do this. I suggest exhausting your options there first.

Good luck.

Brandon
  • 4,491
  • 6
  • 38
  • 59
  • Hello Brandon thanks for your good helps. I found that when I print a small doc like "hello" in notepad it doesn't go to printer queue and will not appear in **"C:\Windows\System32\spool\PRINTERS"**, but when I click "Print Test Page", I see that in **printer queue** and two files _(.SHD & .SPL)_ in **"spool\Printers"** folder. I used `purgePrinterRes=SetPrinter(printerhandle, 0, nullptr_t, PRINTER_CONTROL_PURGE);` to purge printer. it first deletes _.SHD_ file and after 60 seconds job will remove from queue and folder completely. Then I put the paper and that prints that job incomplete! – user3224020 Feb 03 '14 at 15:28
  • Unfortunately I'm not familiar with the print spool folders. Did you try the suggestion I gave? – Brandon Feb 03 '14 at 16:36
  • For some reasons no. I think purge is like cancel all jobs but simpler way. my problem is solving! if i stop "windows _print spooler_ service" then purge printer then start spooler service, purge will happen very faster. and the reason of printing a incomplete document after purging is that: my printer (Tally T5040) have an internal cache that caches some of document separate from windows print spooler! now I'm searching in printer manual for a solution. I'll explain if success. Thanks. – user3224020 Feb 03 '14 at 18:29