如何使用网络路径获取默认打印机名称

时间:2009-03-25 09:11:04

标签: c# vb.net

我想通过网络路径获取默认的打印机名称。因为我使用网络打印机作为默认打印机。所以我需要在VB.NET或C#.Net中使用它。需要善意的帮助。提前致谢

Sivakumar.P

2 个答案:

答案 0 :(得分:34)

尝试枚举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;
}

答案 1 :(得分:1)

这不太好用。

我在更多机器上有更好的体验
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));
}