2012-06-15 2 views

답변

2

.net의 일부인 System.Management dll을 사용할 수 있습니다.

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

다음을 선택합니다 : 오른쪽

2 "참조 추가"를 선택 SolutionExplorer 창에서 참조 항목을 클릭합니다. NET 탭과 라이브러리 System.Mamagement를 검색하여 선택 (보도를 확인!)

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에 아주 새롭기 때문에 당신이 말하는 것을 이해할 수 없다. 전체 코드를 게시 해주세요. 제 컴퓨터에 설치된 모든 프린터의 이름을 가져야합니다. 제발 도와주세요. – user777574

관련 문제