如何在 WPF 中获取打印机的名称和端口名称
本文关键字:打印机 WPF 获取 | 更新日期: 2023-09-27 18:37:11
我是WPF的新手。我想将打印机名称和端口(当前连接到我的系统)添加到组合框中。
有人可以给我看一个简单的解决方案吗?
您可以使用 System.Management dll,它是 .net 的一部分。
using System.Management;
ManagementObjectSearcher printers= new ManagementObjectSearcher("Select Name from Win32_Printer");
foreach (ManagementObject printer in printers.Get())
Console.WriteLine("Name: {0}", printer.GetPropertyValue("Name"));
如果您需要除名称之外的更多信息,则可以使用以下信息(从此处获取。
class Win32_Printer : CIM_Printer
{
uint32 Attributes;
uint16 Availability;
string AvailableJobSheets[];
uint32 AveragePagesPerMinute;
uint16 Capabilities[];
string CapabilityDescriptions[];
string Caption;
string CharSetsSupported[];
string Comment;
uint32 ConfigManagerErrorCode;
boolean ConfigManagerUserConfig;
string CreationClassName;
uint16 CurrentCapabilities[];
string CurrentCharSet;
uint16 CurrentLanguage;
string CurrentMimeType;
string CurrentNaturalLanguage;
string CurrentPaperType;
boolean Default;
uint16 DefaultCapabilities[];
uint32 DefaultCopies;
uint16 DefaultLanguage;
string DefaultMimeType;
uint32 DefaultNumberUp;
string DefaultPaperType;
uint32 DefaultPriority;
string Description;
uint16 DetectedErrorState;
string DeviceID;
boolean Direct;
boolean DoCompleteFirst;
string DriverName;
boolean EnableBIDI;
boolean EnableDevQueryPrint;
boolean ErrorCleared;
string ErrorDescription;
string ErrorInformation[];
uint16 ExtendedDetectedErrorState;
uint16 ExtendedPrinterStatus;
boolean Hidden;
uint32 HorizontalResolution;
datetime InstallDate;
uint32 JobCountSinceLastReset;
boolean KeepPrintedJobs;
uint16 LanguagesSupported[];
uint32 LastErrorCode;
boolean Local;
string Location;
uint16 MarkingTechnology;
uint32 MaxCopies;
uint32 MaxNumberUp;
uint32 MaxSizeSupported;
string MimeTypesSupported[];
string Name;
string NaturalLanguagesSupported[];
boolean Network;
uint16 PaperSizesSupported[];
string PaperTypesAvailable[];
string Parameters;
string PNPDeviceID;
string PortName;
uint16 PowerManagementCapabilities[];
boolean PowerManagementSupported;
string PrinterPaperNames[];
uint32 PrinterState;
uint16 PrinterStatus;
string PrintJobDataType;
string PrintProcessor;
uint32 Priority;
boolean Published;
boolean Queued;
boolean RawOnly;
string SeparatorFile;
string ServerName;
boolean Shared;
string ShareName;
boolean SpoolEnabled;
datetime StartTime;
string Status;
uint16 StatusInfo;
string SystemCreationClassName;
string SystemName;
datetime TimeOfLastReset;
datetime UntilTime;
uint32 VerticalResolution;
boolean WorkOffline;
};
编辑:为了帮助理解这个答案,我将逐步介绍。(创建一个新的 WPF 应用程序,确保它被称为 PrinterDisplay,我将从空白开始)
1:右键单击"解决方案资源管理器"窗口中的"引用"项,选择"添加引用"
2:选择 .NET 选项卡并搜索库 System.Mamagement 并选择它(按确定!
3:将此代码粘贴到主窗口中.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Management;
using System.Collections.ObjectModel;
namespace PrinterDisplay
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<SystemPrinter> Printers
{
get { return (ObservableCollection<SystemPrinter>)GetValue(PrintersProperty); }
set { SetValue(PrintersProperty, value); }
}
public static readonly DependencyProperty PrintersProperty = DependencyProperty.Register("Printers", typeof(ObservableCollection<SystemPrinter>), typeof(MainWindow), new UIPropertyMetadata(null));
public MainWindow()
{
InitializeComponent();
Printers = new ObservableCollection<SystemPrinter>();
ManagementObjectSearcher printers = new ManagementObjectSearcher("Select Name, PortName from Win32_Printer");
foreach (ManagementObject printer in printers.Get())
this.Printers.Add(new SystemPrinter()
{
Name = (string)printer.GetPropertyValue("Name"),
Port = (string)printer.GetPropertyValue("PortName"),
});
}
}
public class SystemPrinter
{
public string Name { get; set; }
public string Port { get; set; }
}
}
4:将此代码粘贴到 MainWindow.xam 中
<Window x:Class="PrinterDisplay.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView ItemsSource="{Binding Printers}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Port}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
5:按 F5 并惊叹于打印机和端口列表。