C# でプリンタ名を取得する方法

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

ドキュメントを印刷に送る.NETアプリでは、システム上で使用可能なプリンタを把握することが一般的な前提条件です。 ユーザーにドロップダウンからプリンタを選ばせることが目的であっても、特定のデバイスに印刷ジョブを自動でルートさせることが目的であっても、プログラムでプリンタ名を取得することが第一歩です。

IronPrint は、現在の Windows マシンにインストールされているすべてのプリンターを List<string> として返す単一の静的メソッド — Printer.GetPrinterNames() — を提供します。 以下では、インストール、同期および非同期の取得、選択したプリンタ名を印刷ジョブに入力する方法について説明します。

クイックスタート: プリンタ名を取得

  1. NuGet 経由で IronPrint をインストールします: Install-Package IronPrint
  2. ファイルに using IronPrint; を追加してください
  3. Printer.GetPrinterNames() を呼び出して、プリンター名の List<string> を取得します
  4. リストを反復処理し、各名前を表示または保存する
  5. PRINT時に選択した名前を PrintSettings.PrinterName に渡す
  1. IronPrint をNuGetパッケージマネージャでインストール

    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() はオペレーティングシステムに問い合わせを行い、登録されているすべてのプリンターを 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
$vbLabelText   $csharpLabel

コンソール出力

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
$vbLabelText   $csharpLabel

私たちは 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)
$vbLabelText   $csharpLabel

PrintSettings は、Grayscale、および PaperMargins などの追加プロパティをサポートしています。 完全なリストはPrintSettings API reference印刷設定ハウツーガイドでご覧ください。

また、Printer.GetPrinterTrays(printerName) を使用して、指定されたプリンターで利用可能な用紙トレイを取得します。これは、印刷ジョブで特定のトレイから用紙を取り出す必要がある場合に便利です。

次のステップは何ですか?

ここでは、IronPrintのインストール、PrintSettings.PrinterNameを使用した特定のプリンターへのドキュメントのルーティングという4つの操作について説明しました。

さらに詳しい内容や例については、これらのリソースをご参照ください:

無料トライアルライセンスを取得して、ライブ環境ですべての機能をテストするか、ライセンスオプションを見ることがサービスの準備が整ったときに可能です。

よくある質問

C#でプリンター名を取得する最も簡単な方法は?

C#でプリンター名を取得する最も簡単な方法は、IronPrint .NETを使用することです。これにより、単一のメソッド呼び出しでインストール済みのプリンターのリストを取得できます。

IronPrintを使用して非同期でプリンター名を取得できますか?

はい、IronPrint .NETは非同期操作をサポートしており、メインスレッドをブロックせずにプリンター名を取得できます。

IronPrintを使用して特定のプリンターに名前で印刷できますか?

もちろん、IronPrint .NETを使用すると、プリンター名を指定して任意のインストール済みプリンターにドキュメントを直接印刷できます。

IronPrintはすべて for .NETプラットフォームでプリンター名を取得できますか?

IronPrint .NETはさまざまな.NETプラットフォームで動作するように設計されており、異なる環境でシームレスにプリンター名を取得できます。

IronPrintはネットワーク環境でプリンター名をどのように処理しますか?

IronPrint .NETは、ローカルプリンターとネットワークプリンターの名前を取得できるため、さまざまなネットワーク構成での使用に便利です。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 41,154 | バージョン: 2026.5 just released
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package IronPrint
サンプルを実行する プリンターに出力されるドキュメントを見る。