什么是获得在.NET中的默认打印机的最佳方式打印机、方式、NET

2023-09-02 01:52:52 作者:温柔如初

我需要得到默认的打印机名称。我会使用C#,但我怀疑这更多的是一个框架的问题,而不是特定的语言。

I need to get the default printer name. I'll be using C# but I suspect this is more of a framework question and isn't language specific.

推荐答案

我发现的最简单的方法是创建一个新的 PrinterSettings 对象。它从所有默认值,这样你就可以检查它的名称的属性来获取默认打印机的名称。

The easiest way I found is to create a new PrinterSettings object. It starts with all default values, so you can check its Name property to get the name of the default printer.

PrinterSettings 在System.Drawing.dll程序命名空间中的 System.Drawing.Printing

PrinterSettings is in System.Drawing.dll in the namespace System.Drawing.Printing.

PrinterSettings settings = new PrinterSettings();
Console.WriteLine(settings.PrinterName);

另外,你也许可以使用静态 PrinterSettings.InstalledPrinters 方法来获得所有的打印机名称的列表,然后设置的的PrinterName 的财产,检查的 IsDefaultPrinter 的。我没有试过,但文档似乎表明它不会工作。显然的 IsDefaultPrinter 的仅仅是真实的,当的的PrinterName 的没有明确设置。

Alternatively, you could maybe use the static PrinterSettings.InstalledPrinters method to get a list of all printer names, then set the PrinterName property and check the IsDefaultPrinter. I haven't tried this, but the documentation seems to suggest it won't work. Apparently IsDefaultPrinter is only true when PrinterName is not explicitly set.