C# 如何使用网络路径获取默认打印机名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/680788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the default printer name with network path
提问by sivakumar
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
我想通过网络路径获取默认打印机名称。因为我使用网络打印机作为默认打印机。所以我需要在 VB.NET 或 C#.Net 中使用它。需要帮助。提前致谢
Sivakumar.P
西瓦库玛
采纳答案by Jim H.
Try enumerating System.Drawing.Printing.PrinterSettings.InstalledPrinters
.
尝试枚举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;
}
回答by anon
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));
}