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.
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
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
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).