如何在C#中检索打印机名称
了解系统上可用的打印机是任何.NET应用程序打印文档的常见先决条件。 无论目标是让用户从下拉列表中选择打印机,还是自动将打印作业路由到特定设备,编程方式检索打印机名称都是第一步。
IronPrint 提供了一个静态方法 — Printer.GetPrinterNames() — 该方法将当前 Windows 机器上已安装的所有打印机作为 List<string> 返回。 我们介绍安装、同步和异步检索以及如何将选定的打印机名称输入到打印作业中。
快速入门:检索打印机名称
- 通过 NuGet 安装 IronPrint:
Install-Package IronPrint - 在文件中添加
using IronPrint; - 调用
Printer.GetPrinterNames()以获取List<string>打印机名称 - 迭代列表并显示或存储每个名称
- PRINT时将选定的名称传递给
PrintSettings.PrinterName
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPrint
PM > Install-Package IronPrint -
复制并运行这段代码。
using IronPrint; // Retrieve every printer installed on this machine List<string> printers = Printer.GetPrinterNames(); foreach (var name in printers) { Console.WriteLine(name); } -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronPrint
最小工作流程(5 个步骤)
- 安装 IronPrint C# 打印库
- 调用
Printer.GetPrinterNames() - 迭代返回的
List - 将名称分配给
PrintSettings.PrinterName以定位该打印机 - 将配置的设置传递给
Printer.Print()进行打印
如何列出所有安装的打印机名称?
Printer.GetPrinterNames() 会查询操作系统,并将所有已注册的打印机作为 List<string> 返回。 我们调用此方法一次,并迭代结果:
:path=/static-assets/print/content-code-examples/how-to/retrieve-printer-names/retrieve-printer-names-list-all-printers.cs
using IronPrint;
using System;
using System.Collections.Generic;
// List every installed printer
List<string> printerNames = Printer.GetPrinterNames();
Console.WriteLine($"Found {printerNames.Count} printer(s):\n");
// Print each printer name
foreach (string name in printerNames)
{
Console.WriteLine($" • {name}");
}
Imports IronPrint
Imports System
Imports System.Collections.Generic
' List every installed printer
Dim printerNames As List(Of String) = Printer.GetPrinterNames()
Console.WriteLine($"Found {printerNames.Count} printer(s):" & vbCrLf)
' Print each printer name
For Each name As String In printerNames
Console.WriteLine($" • {name}")
Next
控制台输出
找到3台打印机:
• Microsoft Print to PDF
• HP LaserJet Pro MFP M428
• OneNote(桌面)
返回的列表包括本地打印机、网络打印机和虚拟打印驱动程序。 每个字符串与Windows设置 > 打印机和扫描仪面板中显示的名称完全匹配,因此可以直接用于打印设置配置。
如果未安装打印机,该方法返回一个空列表而不是抛出异常。 在向用户展示选项之前,只需进行一次快速 printerNames.Count 检查即可。
如何异步检索打印机名称?
对于不允许阻塞 UI 线程的应用程序(如 WPF、MAUI 或 ASP.NET Web 应用程序),IronPrint 提供了 Printer.GetPrinterNamesAsync()。 该方法返回 Task<List<string>>,其工作原理与其异步对应版本完全一致:
:path=/static-assets/print/content-code-examples/how-to/retrieve-printer-names/retrieve-printer-names-async-printer-names.cs
using IronPrint;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
// Retrieve printer names asynchronously
List<string> printerNames = await Printer.GetPrinterNamesAsync();
// Print each printer name
foreach (string name in printerNames)
{
Console.WriteLine(name);
}
Imports IronPrint
Imports System
Imports System.Collections.Generic
Imports System.Threading.Tasks
' Retrieve printer names asynchronously
Dim printerNames As List(Of String) = Await Printer.GetPrinterNamesAsync()
' Print each printer name
For Each name As String In printerNames
Console.WriteLine(name)
Next
我们 await 像调用其他异步 API 一样调用它。 结果与 List<string> 返回的 GetPrinterNames() 相同,因此无需进行额外的解析或转换。 此异步模式可与 async Task 控制器操作和 async void 事件处理程序自然集成。
如何通过名称打印到指定打印机?
获取打印机名称后,将其赋值给 PrintSettings.PrinterName,并将设置对象传递给 Printer.Print()。 这将文档直接发送到选择的打印机而不显示对话框:
:path=/static-assets/print/content-code-examples/how-to/retrieve-printer-names/retrieve-printer-names-print-to-specific-printer.cs
using IronPrint;
using System.Collections.Generic;
// Retrieve available printers
List<string> printers = Printer.GetPrinterNames();
// Select a printer matching "LaserJet", or fall back to the first available
string targetPrinter = printers.Find(p => p.Contains("LaserJet"))
?? printers[0];
// Configure print settings
PrintSettings settings = new PrintSettings
{
PrinterName = targetPrinter,
PaperSize = PaperSize.A4,
NumberOfCopies = 1
};
// Print the document
Printer.Print("invoice.pdf", settings);
Imports IronPrint
Imports System.Collections.Generic
' Retrieve available printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
' Select a printer matching "LaserJet", or fall back to the first available
Dim targetPrinter As String = printers.Find(Function(p) p.Contains("LaserJet")) _
OrElse printers(0)
' Configure print settings
Dim settings As New PrintSettings With {
.PrinterName = targetPrinter,
.PaperSize = PaperSize.A4,
.NumberOfCopies = 1
}
' Print the document
Printer.Print("invoice.pdf", settings)
PrintSettings 支持其他属性,例如 Grayscale 和 PaperMargins。 请参阅PrintSettings API参考中的完整列表及打印设置操作指南。
我们还通过 Printer.GetPrinterTrays(printerName) 获取指定打印机的可用纸盒,当打印任务需要从特定纸盒取纸时,此功能非常有用。
我的下一个步骤是什么?
我们涵盖了四项操作:安装 IronPrint、使用 GetPrinterNames() 列出所有打印机名称、使用 GetPrinterNamesAsync() 异步执行同一查询,以及通过 PrintSettings.PrinterName 将文档路由到特定打印机。
欲了解更多阅读和更深入的示例,请探索这些资源:
- IronPrint 教程 — 文档打印,提供端到端打印操作指南。 参见打印设置指南,以配置DPI、边距、方向等。
- Printer类API参考 应用于静态方法的完整列表。
常见问题解答
在C#中获取打印机名称的最简单方法是什么?
在C#中获取打印机名称的最简单方法是使用 IronPrint .NET,它允许您通过单一方法调用获取已安装的打印机列表。
我可以使用 IronPrint 异步检索打印机名称吗?
是的,IronPrint .NET 支持异步操作,允许您在不阻塞主线程的情况下检索打印机名称。
可以使用 IronPrint 根据名称打印到特定的打印机吗?
当然可以,IronPrint .NET 允许您指定打印机名称,因此您可以将文档直接打印到您选择的任何已安装打印机。
IronPrint 是否支持在所有 .NET 平台上检索打印机名称?
IronPrint .NET 旨在与各种 .NET 平台配合使用,使您能够在不同的环境中无缝检索打印机名称。
IronPrint 如何在网络环境中处理打印机名称?
IronPrint .NET 可以从本地和网络打印机中检索打印机名称,使其在不同网络配置中都能灵活使用。

