chrisanderson


In vb.net i want to get the name of the default printer. I have found the following c# code but dont understand c#. Can anyone help me out translating this to vb

Many thanks

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));
}



Re: Get Default Printer

Richard_Wolf


No need to resort to API calls. The PrinterSettings object, when created, should be set to the default printer:

Public Shared Function DefaultPrinterName() As String
Dim oPS As New System.Drawing.Printing.PrinterSettings

Try
DefaultPrinterName = oPS.PrinterName
Catch ex As System.Exception
DefaultPrinterName = ""
Finally
oPS = Nothing
End Try
End Function





Re: Get Default Printer

chrisanderson

Richard,

Many thanks for your reply, and that works perfectly!

I thought this should be straight forward, but all my searches didnt find anything.

Thank you again

Chris