获得本机的可用的所有打印机

1.获得本机的可用的所有打印机的列表

using    System.Collections.Specialized;  
  
using    System.Management;  
  使用此方法:  
                  
public     static    StringCollection   GetPrintersCollection()  
                  {  
                          StringCollection   printerNameCollection   
=     new    StringCollection();  
                          
string    searchQuery    =     " SELECT   *   FROM   Win32_Printer " ;  
                          ManagementObjectSearcher   searchPrinters   
=   
                                      
new    ManagementObjectSearcher(searchQuery);  
                          ManagementObjectCollection   printerCollection   
=    searchPrinters.Get();  
                          
foreach    (ManagementObject   printer    in    printerCollection)  
                          {  
                                  printerNameCollection.Add(printer.Properties[
" Name " ].Value.ToString());  
                          }  
                          
return    printerNameCollection;  
                  }  
  获取:  
                          StringCollection   sc   
=    GetPrintersCollection();  
                          
for    ( int    i    =     0 ;   i    <    sc.Count;   i ++ )  
                          {  
                                  
// 得到可用打印机名列表  
                                  MessageBox.Show(sc[i].ToString());  
                          }

 2.设为默认打印机.  

public     static     bool    SetDefaultPrinter( string    printerName)  
                  {  
                          
bool    setDefaulted    =     false ;  
                          ManagementObjectSearcher   query;  
                          ManagementObjectCollection   querycollection;  
                          
string    _classname    =     " select   *   from   win32_printer " ;  
   
                          query   
=     new    ManagementObjectSearcher(_classname);  
                          querycollection   
=    query.Get();  
   
                          
foreach    (ManagementObject   mo    in    querycollection)  
                          {  
                                  
if    ( string .Compare(mo[ " name " ].ToString(),   printerName,    true )    ==     0 )  
                                  {  
                                          mo.InvokeMethod(
" setdefaultprinter " ,    null );  
                                          setDefaulted   
=     true ;  
                                          
break ;  
                                  }  
                          }  
                          
return    setDefaulted;  
                  }

 

你可能感兴趣的:(打印)