CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    112

    Get list of connected printers

    Hi, I'm writing an application where I need to print out labels to a label printer. In my application I have it look for a printer with the name "DYMO Label Writer 450", and then use that printer. The problem I'm running in to is that if some how the printer's USB becomes unplugged and plugged back in, it might make a copy called "DYMO Label Writer 450 (Copy 1)". Now "DYMO Label Writer 450" is not connected so it won't work, and "DYMO Label Writer 450 (Copy 1)" is not equal to "DYMO Label Writer 450", so that won't work. Is it possible to get a list of all printers that are connected to the computer, and have it print to a printer name that contains "DYMO Label Writer 450" rather than being equal to it?

    Here is the code I use to get the printer name and print:

    Code:
                try
                {
                    PrintDocument printDoc = new PrintDocument();
                    // use the PrintDocument Class to Set the parameters
                    printDoc.PrinterSettings.PrinterName = "DYMO LabelWriter 450";
                    printDoc.DefaultPageSettings.Landscape = true;
                    if ("DYMO LabelWriter 450" != "***None***")
                    {
                        printDoc.PrintController = new System.Drawing.Printing.StandardPrintController();
                        printDoc.PrintPage += new PrintPageEventHandler(printDoc_Print);
                        printDoc.Print();
                    }
                }
                catch (Exception ex)
                {
                    LogError(ex, "7");
                }
    Thank you.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  2. #2
    Join Date
    May 2002
    Location
    Boston
    Posts
    67

    Cool Re: Get list of connected printers

    Hi

    try this

    Code:
    using System.Drawing.Printing;
    using System.Diagnostics;
    
            //
            //
            //......
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                try
                {
                    foreach (string InstalledPrinters in PrinterSettings.InstalledPrinters  )
                    {
                        Debug.WriteLine(InstalledPrinters);
                    }
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
    Curt

  3. #3
    Join Date
    May 2002
    Location
    Boston
    Posts
    67

    Re: Get list of connected printers

    Hi,

    After posting this I realised this would just give you a list of installed printers.

    try this

    http://www.codeproject.com/Articles/...nected-using-C

    Curt

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Get list of connected printers

    You can get a list of printers from the following code:
    Code:
    var printServer = new PrintServer();
    
    foreach(var printer in printServer.GetPrintQueues())
    {
      Console.WriteLine(printer.Name);
    }
    Given that, your code may not be that robust if you rely on the printer name (because the user may install the printer with a name that is different than "DYMO Label Writer 450").

    You can certainly attempt to use a printer with this name, but if you can't find it, rather than guess what the name might be, you may want to display a PrintDialog to the user and have the user select the printer (which you can store and use the next time the user wants to print).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured