zoukankan      html  css  js  c++  java
  • 获得本机的可用的所有打印机

    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;  
                      }
  • 相关阅读:
    如何规范自己的编程以及软件开发目录(二)
    关于README的内容
    关于编程编程规范以及开发目录的规范性
    第五章:条件、循环以及其他语句(上)
    第四章 当索引行不通时
    python-zipfile模块
    python-shutil模块
    python-sys模块
    python-os模块
    python-threading.Thread(多线程)
  • 原文地址:https://www.cnblogs.com/tonybinlj/p/1391248.html
Copyright © 2011-2022 走看看