C# でプリンタ名を取得する方法
ドキュメントを印刷に送る.NETアプリでは、システム上で使用可能なプリンタを把握することが一般的な前提条件です。 ユーザーにドロップダウンからプリンタを選ばせることが目的であっても、特定のデバイスに印刷ジョブを自動でルートさせることが目的であっても、プログラムでプリンタ名を取得することが第一歩です。
IronPrintは、現在のWindowsマシンにインストールされているすべてのプリンターをPrinter.GetPrinterNames()を公開しています。 以下では、インストール、同期および非同期の取得、選択したプリンタ名を印刷ジョブに入力する方法について説明します。
- NuGetを使用してIronPrintをインストールします:
Install-Package IronPrint - ファイルに
using IronPrint;を追加します - プリンター名の
Printer.GetPrinterNames()を呼び出します - リストを反復処理し、各名前を表示または保存する
- 印刷時に選択した名前を
PrintSettings.PrinterNameに渡します
-
1Install IronPrint with NuGet Package Manager
-
2このコード スニペットをコピーして実行します。
using IronPrint; // Retrieve every printer installed on this machine List<string> printers = Printer.GetPrinterNames(); foreach (var name in printers) { Console.WriteLine(name); }Imports IronPrint ' Retrieve every printer installed on this machine Dim printers As List(Of String) = Printer.GetPrinterNames() For Each name In printers Console.WriteLine(name) Next nameImports IronPrint ' Retrieve every printer installed on this machine Dim printers As List(Of String) = Printer.GetPrinterNames() For Each name In printers Console.WriteLine(name) Next name -
3実際の環境でテストするためにデプロイする
今日プロジェクトで IronPrint を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- IronPrint C#印刷ライブラリをインストール
Printer.GetPrinterNames()を呼び出す- 返された
List<string>を反復処理する PrintSettings.PrinterNameに名前を割り当て、そのプリンタをターゲットにする- 設定された設定を
Printer.Print()に渡して印刷する
インストールされたすべてのプリンタ名を列挙するには?
List<string>として返します。 このメソッドを一度呼び出し、結果を反復処理します:
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}");
}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}")
NextImports 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ウェブアプリ - IronPrintはPrinter.GetPrinterNamesAsync()を提供します。 メソッドはTask<List<string>>を返し、その同期カウンターパートと同様に機能します:
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);
}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)
NextImports 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他の非同期APIと同様にawaitで呼び出しを行います。 結果はList<string>であるため、追加の解析や変換は必要ありません。 この非同期パターンはasync voidイベントハンドラーと自然に統合します。
名前で特定のプリンタに印刷するには?
プリンター名を取得したら、それをPrinter.Print()に渡します。 これにより、ダイアログを表示せずに選択されたプリンタにドキュメントが直接送信されます:
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);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)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)PaperMarginsのような追加のプロパティをサポートしています。 完全なリストはPrintSettings API referenceと印刷設定ハウツーガイドでご覧ください。
特定のトレイから紙を引き出す必要がある印刷ジョブで便利なPrinter.GetPrinterTrays(printerName)を使用して、指定されたプリンターの利用可能な紙トレイも取得します。
次のステップは何ですか?
以下の四つの操作をカバーしました:IronPrintのインストール、PrintSettings.PrinterNameを通じた特定のプリンターへのドキュメントのルーティング。
さらに詳しい内容や例については、これらのリソースをご参照ください:
- IronPrintチュートリアル - 印刷文書でエンドツーエンドの印刷手順を説明します。
- PrintSettings使い方について、DPI、マージン、オリエンテーションなどの設定方法。
- 静的メソッドの完全なリストについては、プリンタ クラス API リファレンスをご覧ください。
無料トライアルライセンスを取得して、ライブ環境ですべての機能をテストするか、ライセンスオプションを見ることがサービスの準備が整ったときに可能です。
よくある質問
C#でプリンター名を取得する最も簡単な方法は?
C#でプリンター名を取得する最も簡単な方法は、IronPrint .NETを使用することです。これにより、単一のメソッド呼び出しでインストール済みのプリンターのリストを取得できます。
IronPrintを使用して非同期でプリンター名を取得できますか?
はい、IronPrint .NETは非同期操作をサポートしており、メインスレッドをブロックせずにプリンター名を取得できます。
IronPrintを使用して特定のプリンターに名前で印刷できますか?
もちろん、IronPrint .NETを使用すると、プリンター名を指定して任意のインストール済みプリンターにドキュメントを直接印刷できます。
IronPrintはすべて for .NETプラットフォームでプリンター名を取得できますか?
IronPrint .NETはさまざまな.NETプラットフォームで動作するように設計されており、異なる環境でシームレスにプリンター名を取得できます。
IronPrintはネットワーク環境でプリンター名をどのように処理しますか?
IronPrint .NETは、ローカルプリンターとネットワークプリンターの名前を取得できるため、さまざまなネットワーク構成での使用に便利です。
What additional properties can I configure with PrintSettings?
With PrintSettings, you can configure properties like Dpi, PaperSize, PaperOrientation, Grayscale, and PaperMargins.
How do I iterate over printer names in C# using IronPrint?
You can use a foreach loop to iterate over the list returned by Printer.GetPrinterNames(), allowing you to display or store each printer name.
Can IronPrint work with network and virtual printers?
Yes, the list returned by Printer.GetPrinterNames() includes local printers, network printers, and virtual print drivers.
Where can I find more information on printing settings in IronPrint?
You can refer to the [PrintSettings How-To Guide](https://ironsoftware.com/csharp/print/how-to/print-settings/) and the [API Reference](https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.PrintSettings.html) for detailed information.
Does IronPrint provide support for different paper trays?
Yes, you can use Printer.GetPrinterTrays(printerName) to retrieve available paper trays for a given printer.
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。