Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the default printer name with network path

Tags:

c#

vb.net

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

like image 793
sivakumar Avatar asked Mar 25 '09 09:03

sivakumar


2 Answers

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;
}
like image 112
Jim H. Avatar answered Oct 19 '22 09:10

Jim H.


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));
}
like image 32
anon Avatar answered Oct 19 '22 08:10

anon