How to get the default printer name with network path


Question

I want to get the default printer name with the network path. Because i am using the network printer as a default printer. So i need this in VB.NET or C#.Net. Kind help needed. Thanks in advance

Sivakumar.P

1
10
3/25/2009 9:11:04 AM

Accepted Answer

Try enumerating System.Drawing.Printing.PrinterSettings.InstalledPrinters.

using System.Drawing.Printing;
string GetDefaultPrinter()
{
    PrinterSettings settings = new PrinterSettings();
    foreach (string printer in PrinterSettings.InstalledPrinters)
    {
        settings.PrinterName = printer;
        if (settings.IsDefaultPrinter)
            return printer;
    }
    return string.Empty;
}
34
3/25/2009 12:06:34 PM

This does not work too well. I had better experience on more machines with

DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int size);

StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;
if (GetDefaultPrinter(dp, ref size)) {
        Console.WriteLine(String.Format("Printer: {0}, name length {1}", dp.ToString().Trim(), size));
} else {
    int rc = GetLastError();
    Console.WriteLine(String.Format("Failed. Size: {0}, error: {1:X}", size, rc));
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon