How to get/set Default Printer in WPF (i.e. System.Printing namespace) RRS feed

  • Question

  • I'm looking for the method to determine the default printer in the WPF (System.Printing) namespace. This namespace provides many details about print servers, queues and jobs, but not the printer itself.

    System.Drawing.Printing namespace seems to be geared for WinForms...

    I don't like all the magic strings in WMI. I'm just looking for an object that already abstracts all the details.


    If I can stretch this question a bit further, I'm also looking for the printer status ("Offline") in the System.Printing namespace... but that namespace is geared towards print spools, queues and jobs...


    thx sbc

    • Moved by ErikEJMVP Wednesday, June 30, 2010 5:33 PM wrong forum (From:SQL Server Compact)
    Wednesday, June 30, 2010 4:40 PM

All replies

  • You can use the System.Windows.Controls.PrintDialog




    Wednesday, June 30, 2010 6:18 PM
  • Using System.Printing, you can get a PrintQueue. 


    PrintQueue printer = LocalPrintServer.GetDefaultPrintQueue();


    More on PrintQueue



    Matt Hohn

    Wednesday, June 30, 2010 6:23 PM
  • Follow up question (I see a similar post, but with no answer).


    Why is the PrintQueue status always None?


    Refresh() method does not affect the property value.


    Clarification - the PrintQueueStatus is always None, whether the printer is plugged in or not. Also, I can cause other events which should bubble up thru this property - paper out, init, no ink etc... but alas, always None.


    Wednesday, June 30, 2010 7:55 PM
  • PrintQueue is not always none.  I just tested this out targeting a printer that does not have any paper.


    PrintQueue printer = LocalPrintServer.GetDefaultPrintQueue();

    Console.WriteLine("STATUS : {0}", printer.QueueStatus);



    STATUS : PaperOut


    You could also try to target a specific print queue


    PrintQueue printer = new PrintQueue(new PrintServer(), "Xerox WorkCentre 5655 PS");


    new PrintServer() with no arguments will target the local machine.




    More info on your scenario, or code samples to look at might help diagnose the problem..



    Matt Hohn
    Wednesday, June 30, 2010 8:52 PM
  • Matt,


    The following code (using two methods) produces None for printers which are connected or not. I had failed to mention earlier that the OS is XPe (aka WES 9). I wonder if something at a lower layer in the printer modules is missing...



    	static void Main(string[] args)
    			PrintServer myPrintServer = new PrintServer(new LocalPrintServer().Name);
    			// List the print server's queues
    			PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
    			//String printQueueNames = "My Print Queues:\n\n";
    			foreach (PrintQueue pq in myPrintQueues)
    				Console.WriteLine("Name: " + pq.Name);
    				Console.WriteLine("Location: " + pq.Location);
    				Console.WriteLine("Offline: " + pq.IsOffline);
    				Console.WriteLine("Status: " + pq.QueueStatus.ToString());
    PrintQueue printer = new PrintQueue(new PrintServer(), "HP OfficeJet H470 series"); Console.WriteLine("\nNew Method Status: " + printer.QueueStatus.ToString()); Console.Read(); }


    Name: Microsoft XPS Document Writer
    Offline: False
    Status: None
    Name: HP Officejet H470 series (Copy 1)
    Offline: False
    Status: None
    Name: HP Officejet H470 series
    Offline: False
    Status: None
    Name: Brother PocketJet 3PlusB
    Offline: False
    Status: None
    New Method Status: None

    Wednesday, June 30, 2010 11:35 PM
  • Anyone with thoughts on this?
    Friday, July 9, 2010 4:49 PM
  • PrintQueue printer = LocalPrintServer .GetDefaultPrintQueue();

    Console .WriteLine( "STATUS : {0}" , printer.QueueStatus);



    STATUS : None


    You could also try to target a specific print queue


    PrintQueue printer = new PrintQueue (new PrintServer (), "Xerox WorkCentre 5655 PS" );



    Both of these samples produce None as the status. Both on Win XP desktop and device with XPe.


    Any thoughts??

    Wednesday, July 14, 2010 11:55 PM
  • Hi SBC:

    "Error" is returned if a job is sent to a printer and "Use Printer Offline" option for the printer is not checked.
    So it is working for that case.

    "Offline" is not returned for the printer that Control Panel reports as Offline.

    There is an old KB that seems relevant: http://support.microsoft.com/kb/160129.
    The sample in the KB behaves the same as your WPF sample.


    For more information, your question falls into the paid support category which requires a more in-depth level of support.  Please visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone


    Saturday, July 17, 2010 12:41 AM