How to get the Computer Name and IP to which a Printer is Connected in LAN

前端 未结 3 2045
[愿得一人]
[愿得一人] 2020-12-18 12:38

In a LAN normally printers are shared, and we can add these shared computers in the LAN to our machine through Windows \"Add Remote Printer\". I want to get the list of adde

3条回答
  •  半阙折子戏
    2020-12-18 13:16

    Using WMI is the common option for this kind of task...

        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
    
                    foreach (ManagementObject printer in searcher.Get())
                    {
                        string printerName = printer["Name"].ToString().ToLower();
                        Console.WriteLine("Printer :" + printerName);
                        PrintProps(printer, "Status");
                        PrintProps(printer, "PrinterState");
                        PrintProps(printer, "PrinterStatus");
    
    
                    }
    
    }
    
    static void PrintProps(ManagementObject o, string prop)     
    {         
            try { Console.WriteLine(prop + "|" + o[prop]); 
            }         
            catch (Exception e) 
            { 
                Console.Write(e.ToString()); 
            }     
    }   
    

提交回复
热议问题