我是WPF的新手。我想将打印机名称和端口(当前连接到我的系统)添加到组合框中。
有人可以给我一个简单的解决方案吗?
答案 0 :(得分:3)
您可以使用.net。
中的System.Management dllusing System.Management;
ManagementObjectSearcher printers= new ManagementObjectSearcher("Select Name from Win32_Printer");
foreach (ManagementObject printer in printers.Get())
Console.WriteLine("Name: {0}", printer.GetPropertyValue("Name"));
如果您需要的信息不仅仅是名称,还可以获得以下信息(取自here。)
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:右键单击SolutionExplorer窗口中的References项,选择“Add reference”
2:选择.NET选项卡并搜索库System.Mamagement并选择它(按ok!)
3:将此代码粘贴到MainWindow.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并惊叹于打印机和端口列表。