Is there a possibility to differ virtual printer from physical one?

后端 未结 3 2078
栀梦
栀梦 2020-12-02 01:48

I have a list of all printers available in WinXP. I need the code (ideally .NET) to filter out all the virtual printers from this list. Is it possible to do? I analyzed all

3条回答
  •  孤街浪徒
    2020-12-02 02:12

    I have a project to collect hardware information and after testing the HiTech answer I see some of old printers (for example HP 2014 on Windows 10) that connect with LPT have WINPRINT PrintProcessor and these printers are connected diectly to computer and not virtual. So I combined the Local, Network and PortName properties (on offer Jerry Coffin answer) to find more accurate local and network printers(not virtual printers).

    using System.Management;
    
    class Printer
    {
        public string Name { get; set; }
        public string Status { get; set; }
        public bool Default { get; set; }
        public bool Local { get; set; }  
        public bool Network { get; set; }        
        public string PrintProcessor { get; set; }
        public string PortName { get; set; }
    }
    
    private void btnGetPrinters_Click(object sender, EventArgs e)
    {          
        List printers = new List();
        var query = new ManagementObjectSearcher("SELECT * from Win32_Printer");
        foreach (var item in query.Get())
        {
            string portName = item["PortName"].ToString().ToUpper();
            if (((bool)item["Local"]==true || (bool)item["Network"]==true) &&  (portName.StartsWith("USB") || portName.StartsWith("LPT")))
            {
                Printer p = new Models.Printer();
                p.Name = (string)item.GetPropertyValue("Name");
                p.Status = (string)item.GetPropertyValue("Status");
                p.Default = (bool)item.GetPropertyValue("Default");
                p.Local = (bool)item.GetPropertyValue("Local");
                p.Network = (bool)item.GetPropertyValue("Network");                    
                p.PrintProcessor = (string)item.GetPropertyValue("PrintProcessor");
                p.PortName = (string)item.GetPropertyValue("PortName");
                printers.Add(p);
            }
        }
    
        // Show on GridView 
        gv.DataSource = printers;
    }
    

    This method works for the printers that connect with USB and LPT. I don't have any idea about other ports (like some faxes port).

提交回复
热议问题