美文网首页
C#使用WMI设置和获取默认打印机

C#使用WMI设置和获取默认打印机

作者: xmsec | 来源:发表于2016-07-14 22:37 被阅读0次
    • 设置默认打印机
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Management;
    protected int SetDefaultPrinter(string PrinterName)  
    {  
            int reint = 0;    
            ManagementObjectSearcher query;  
            ManagementObjectCollection queryCollection;  
            string _classname = "SELECT * FROM Win32_Printer";  
            query = new ManagementObjectSearcher(_classname);  
            queryCollection = query.Get();  
            foreach (ManagementObject printer in queryCollection)  
            {  
                if (string.Compare(printer["Name"].ToString(), PrinterName, true) == 0)  
                {  
                    mo.InvokeMethod("SetDefaultPrinter", null);  
                    reint = 1;  
                    break;  
                }  
            }
    
    • 获取默认打印机
    public static String GetDefaultPrinters()
     {
                string searchQuery = "SELECT * FROM Win32_Printer",DefaultPrinterName;
           ManagementObjectSearcher searchPrinters = 
                new ManagementObjectSearcher(searchQuery);  //exec WQL
           ManagementObjectCollection printerCollection = 
                searchPrinters.Get();  // get a set of object of managementobject
           foreach (ManagementObject printer in printerCollection)
                {
                if ((bool)printer.GetPropertyValue("default") == true)  
                    //judge if the current print is the default printer 
                    {                   
                        DefaultPrinterName = printer.Properties["Name"].Value.ToString();                                       
                    }                         
                }
                    return DefaultPrinterName;            
            }
    

    相关文章

      网友评论

          本文标题:C#使用WMI设置和获取默认打印机

          本文链接:https://www.haomeiwen.com/subject/pufyjttx.html