如何在 C# 中检索打印机名称

This article was translated from English: Does it need improvement?
Translated
View the article in English

Knowing which printers are available on a system is a common prerequisite for any .NET application that sends documents to print. Whether the goal is to let users pick a printer from a dropdown or to route print jobs to a specific device automatically, retrieving printer names programmatically is the first step.

IronPrint exposes a single static method — Printer.GetPrinterNames() — that returns every installed printer on the current Windows machine as a List<string>. We cover installation, synchronous and asynchronous retrieval, and how to feed a selected printer name into a print job below.

Quickstart: Retrieve Printer Names

  1. Install IronPrint via NuGet: Install-Package IronPrint
  2. Add using IronPrint; to the file
  3. Call Printer.GetPrinterNames() to get a List<string> of printer names
  4. Iterate over the list and display or store each name
  5. Pass a selected name to PrintSettings.PrinterName when printing
  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPrint

    PM > Install-Package IronPrint
  2. 复制并运行这段代码。

    using IronPrint;
    
    // Retrieve every printer installed on this machine
    List<string> printers = Printer.GetPrinterNames();
    
    foreach (var name in printers)
    {
        Console.WriteLine(name);
    }
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPrint

    arrow pointer

如何列出所有已安装的打印机名称?

Printer.GetPrinterNames() queries the operating system and returns every registered printer as a List<string>. We call this method once and iterate over the result:

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

// Get every printer registered on this Windows machine
List<string> printerNames = Printer.GetPrinterNames();

Console.WriteLine($"Found {printerNames.Count} printer(s):\n");

foreach (string name in printerNames)
{
    Console.WriteLine($"  • {name}");
}
Imports IronPrint
Imports System
Imports System.Collections.Generic

' Get every printer registered on this Windows machine
Dim printerNames As List(Of String) = Printer.GetPrinterNames()

Console.WriteLine($"Found {printerNames.Count} printer(s):" & vbCrLf)

For Each name As String In printerNames
    Console.WriteLine($"  • {name}")
Next
$vbLabelText   $csharpLabel

控制台输出

Found 3 printer(s):

  • Microsoft Print to PDF
  • HP LaserJet Pro MFP M428
  • OneNote (Desktop)

The returned list includes local printers, network printers, and virtual print drivers. Each string matches the exact name shown in the Windows Settings > Printers & scanners panel, so it can be used directly in print settings configuration.

If no printers are installed, the method returns an empty list rather than throwing an exception. A quick printerNames.Count check is all that's needed before presenting options to a user.

如何异步检索打印机名称?

For applications where blocking the UI thread is not acceptable — WPF, MAUI, or ASP.NET web apps — IronPrint provides Printer.GetPrinterNamesAsync(). The method returns a Task<List<string>> and works identically to its synchronous counterpart:

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

// Await the async call to avoid blocking the UI thread
List<string> printerNames = await Printer.GetPrinterNamesAsync();

foreach (string name in printerNames)
{
    Console.WriteLine(name);
}
Imports IronPrint
Imports System
Imports System.Collections.Generic
Imports System.Threading.Tasks

' Await the async call to avoid blocking the UI thread
Dim printerNames As List(Of String) = Await Printer.GetPrinterNamesAsync()

For Each name As String In printerNames
    Console.WriteLine(name)
Next
$vbLabelText   $csharpLabel

We await the call just like any other async API. The result is the same List<string> returned by GetPrinterNames(), so no additional parsing or conversion is necessary. This async pattern integrates naturally with async Task controller actions and async void event handlers.

如何按名称打印文档到指定打印机?

Once we have the printer name, we assign it to PrintSettings.PrinterName and pass the settings object to Printer.Print(). This sends the document directly to the chosen printer without displaying a dialog:

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

// Step 1 — Retrieve available printers
List<string> printers = Printer.GetPrinterNames();

// Step 2 — Select a printer (first match containing "LaserJet" as an example)
string targetPrinter = printers.Find(p => p.Contains("LaserJet"))
                       ?? printers[0]; // fallback to first available

// Step 3 — Configure print settings
PrintSettings settings = new PrintSettings
{
    PrinterName = targetPrinter,
    PaperSize = PaperSize.A4,
    NumberOfCopies = 1
};

// Step 4 — Print the document
Printer.Print("invoice.pdf", settings);
Imports IronPrint
Imports System.Collections.Generic

' Step 1 — Retrieve available printers
Dim printers As List(Of String) = Printer.GetPrinterNames()

' Step 2 — Select a printer (first match containing "LaserJet" as an example)
Dim targetPrinter As String = printers.Find(Function(p) p.Contains("LaserJet")) _
                       OrElse printers(0) ' fallback to first available

' Step 3 — Configure print settings
Dim settings As New PrintSettings With {
    .PrinterName = targetPrinter,
    .PaperSize = PaperSize.A4,
    .NumberOfCopies = 1
}

' Step 4 — Print the document
Printer.Print("invoice.pdf", settings)
$vbLabelText   $csharpLabel

PrintSettings supports additional properties such as Dpi, PaperOrientation, Grayscale, and PaperMargins. See the full list in the PrintSettings API reference and the print settings how-to guide.

We also retrieve available paper trays for a given printer using Printer.GetPrinterTrays(printerName), which is useful when a print job needs to pull paper from a specific tray.

我的下一个步骤是什么?

We covered four operations: installing IronPrint, listing all printer names with GetPrinterNames(), running the same query asynchronously with GetPrinterNamesAsync(), and routing a document to a specific printer through PrintSettings.PrinterName.

For further reading and deeper examples, explore these resources:

Get a free trial license to test every feature in a live environment, or view licensing options when you're ready to deploy.

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 38,930 | 版本: 2026.4 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPrint
运行示例 观看您的文档打到打印机上。