2012-06-15 69 views

回答

2

您可以使用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; 
}; 

编辑(从here服用。): 来帮助理解这个答案,我会一步一步来。 (创建一个新的WPF应用程序确保它被称为PrinterDisplay,我会从空白开始)

1:右键单击在SolutionExplorer窗口中的参考项,选择“添加引用”

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并惊叹于打印机和端口列表。

+0

,但显示异常'类型'MPS_Printer.MainWindow'上的构造函数的调用匹配指定的绑定约束抛出例外。'行号“3”和行位置“2”。请帮助我 – user777574

+0

您可以从MainWindow类发布您的构造函数代码吗? – Andy

+0

我对wpf很新,所以我不明白你说的是什么,请寄给我完整的code.I必须得到我的机器上所有安装的打印机的名称。请帮助我。 – user777574

相关问题