C# でプリンタ名を取得する方法
ドキュメントを印刷に送る.NETアプリでは、システム上で使用可能なプリンタを把握することが一般的な前提条件です。 ユーザーにドロップダウンからプリンタを選ばせることが目的であっても、特定のデバイスに印刷ジョブを自動でルートさせることが目的であっても、プログラムでプリンタ名を取得することが第一歩です。
IronPrint は単一の静的メソッド Printer.GetPrinterNames() を公開し、現在の Windows マシンにインストールされているすべてのプリンターを List<string> として返します。 以下では、インストール、同期および非同期の取得、選択したプリンタ名を印刷ジョブに入力する方法について説明します。
- NuGet を介して IronPrint をインストールします:
Install-Package IronPrint - ファイルに
using IronPrint;を追加します - プリンター名の
List<string>を取得するためにPrinter.GetPrinterNames()を呼び出します - リストを反復処理し、各名前を表示または保存する
- プリント時に選択した名前を
PrintSettings.PrinterNameに渡します
-
1IronPrint を 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()に渡して印刷する
インストールされたすべてのプリンタ名を列挙するには?
Printer.GetPrinterNames() はオペレーティングシステムをクエリし、すべての登録済みプリンターを 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コンソール出力
Found 3 printer(s):
• Microsoft Print to PDF
• HP LaserJet Pro MFP M428
• OneNote (Desktop)
返されるリストには、ローカルプリンタ、ネットワークプリンタ、仮想印刷ドライバが含まれます。 各文字列は Windows の設定 > プリンターとスキャナーパネルに表示される正確な名前に一致するため、印刷設定の構成で直接使用できます。
プリンタがインストールされていない場合、メソッドは例外を投げるのではなく、空のリストを返します。 ユーザーにオプションを提示する前に、簡単な printerNames.Count チェックだけが必要です。
プリンタ名を非同期で取得するには?
UI スレッドをブロックすることが許可されないアプリケーション、例えば WPF、MAUI、またはASP.NET web apps では、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 を呼び出します。 結果は GetPrinterNames() によって返されるのと同じ List<string> なので、追加の解析や変換は必要ありません。 この非同期パターンは async Task コントローラー アクションおよび async void イベント ハンドラーと自然に統合します。
名前で特定のプリンタに印刷するには?
プリンター名を取得したら、それを PrintSettings.PrinterName に割り当て設定オブジェクトを 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)PrintSettings は、Dpi、PaperOrientation、Grayscale、および PaperMargins のような追加プロパティをサポートします。 完全なリストはPrintSettings APIリファレンスとプリント設定ハウトゥーガイドをご覧ください。
印刷ジョブが特定のトレイから紙を引く必要がある場合に便利な Printer.GetPrinterTrays(printerName) を使用して、特定のプリンターの利用可能な給紙トレイも取得します。
次のステップは何ですか?
当社では、4つの操作について説明しました:IronPrint のインストール、GetPrinterNames() を使用してすべてのプリンタ名をリストすること、GetPrinterNamesAsync() を使用して同じクエリを非同期で実行すること、および PrintSettings.PrinterName を通して特定のプリンタにドキュメントをルーティングすることです。
さらに詳しい内容や例については、これらのリソースをご参照ください:
- IronPrintチュートリアル - ドキュメントを印刷で、エンドツーエンドの印刷手順を確認してください。
- DPI、余白、向きなどを設定するためのPrintSettings ハウトゥー。
- 静的メソッドの完全なリストを含むPrinter Class API リファレンス。
無料トライアルライセンスを取得してすべての機能をライブ環境でテストするか、ライセンスオプションをご覧ください準備ができたら導入してください。
よくある質問
C#でプリンタ名を取得するにはどうすればいいですか?
Windowsマシンでインストールされたプリンタ名のリストを取得するには、IronPrintライブラリとそのメソッドPrinter.GetPrinterNames()を使用できます。
IronPrintを使用して非同期的にプリンタ名を取得する方法はありますか?
はい、IronPrintはPrinter.GetPrinterNamesAsync()を提供しており、UIスレッドをブロックすることなくプリンタ名を取得できます。
C#用のIronPrintライブラリをインストールするにはどうすればよいですか?
IronPrintをインストールするには、.NETプロジェクトでInstall-Package IronPrintというコマンドでNuGetを使用できます。
IronPrintを使用して特定のプリンタに印刷できますか?
はい、プリンタ名を取得した後に、希望のプリンタ名をPrintSettings.PrinterNameに割り当て、Printer.Print()を使用してそのプリンタにドキュメントを送信できます。
プリンタがインストールされていない場合、どうすれば良いですか?
プリンタがインストールされていない場合、Printer.GetPrinterNames()メソッドは空のリストを返します。プリンタ名を使用する前にリストの数を確認する必要があります。
PrintSettingsで構成可能な追加プロパティは何ですか?
PrintSettingsでは、Dpi、PaperSize、PaperOrientation、Grayscale、およびPaperMarginsのようなプロパティを構成できます。
C#でIronPrintを使用してプリンタ名を繰り返し処理するにはどうすればよいですか?
Printer.GetPrinterNames()によって返されるリストをforeachループで繰り返し処理し、各プリンタ名を表示または保存できます。
IronPrintはネットワークプリンタおよび仮想プリンタに対応しますか?
はい、Printer.GetPrinterNames()によって返されるリストには、ローカルプリンタ、ネットワークプリンタ、および仮想プリント ドライバが含まれます。
IronPrintの印刷設定に関する詳細情報はどこで見つけられますか?
[PrintSettings ハウツーガイド](https://ironsoftware.com/csharp/print/how-to/print-settings/)および[APIリファレンス](https://ironsoftware.com/csharp/print/object-reference/api/IronPrint.PrintSettings.html)を参照すると詳細情報が得られます。
IronPrintでは異なる用紙トレイに対応していますか?
はい、指定されたプリンタの利用可能な用紙トレイを取得するには、Printer.GetPrinterTrays(printerName)を使用できます。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。