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

C# 如何使用网络路径获取默认打印机名称,c#,vb.net,C#,Vb.net,我想用网络路径获取默认打印机名称。因为我正在使用网络打印机作为默认打印机。所以我需要在VB.NET或C#.NET中使用它。需要善意的帮助。提前谢谢 Sivakumar.p尝试枚举系统.绘图.打印.打印机设置.已安装打印机 using System.Drawing.Printing; string GetDefaultPrinter() { PrinterSettings settings = new PrinterSettings(); foreach (string print

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


Sivakumar.p

尝试枚举
系统.绘图.打印.打印机设置.已安装打印机

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

这不太管用。我有更好的经验在更多的机器上使用

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