Search code examples
c#network-printers

How to get Printers from Network which is not installed in my system?


I want to search how many printers are available in network. I have checked with Installed printer property and it gives me those printer list which are installed on my system.

I have more than two printers in my network where only one showing in list because it installed on my System.

How to get all printer list from Network who's drivers are not installed on my system or not connected to my system.


Solution

  • I know this post is quite old, but I've been battling with the same issue.

    I eventually managed to solve it and I hope the code below helps someone:

            using(var ds = new DirectorySearcher())
            {
                ds.SearchRoot = new DirectoryEntry("");
                ds.Filter = "(|(&(objectCategory=printQueue)(name=*)))";
    
                ds.PropertiesToLoad.Add("printername");
                ds.PropertiesToLoad.Add("portname");
                ds.PropertiesToLoad.Add("servername");
                ds.PropertiesToLoad.Add("cn");
                ds.PropertiesToLoad.Add("name");
                ds.PropertiesToLoad.Add("printsharename");
                ds.ReferralChasing = ReferralChasingOption.None;
                ds.Sort = new SortOption("name", SortDirection.Descending);
    
                using(var src = ds.FindAll())
                {
                    foreach(SearchResult sr in src)
                    {
                        Console.WriteLine("------------------------------------");
                        Console.WriteLine(sr.GetDirectoryEntry().Name);
                        foreach (DictionaryEntry p in sr.Properties)
                        {
                            var propName = p.Key;
                            var propCollection = (ResultPropertyValueCollection)p.Value;
                            var propValue = propCollection.Count > 0 ? propCollection[0] : "";
                            Console.WriteLine(propName);
                            Console.WriteLine(propValue);
                        }
                        Console.WriteLine("------------------------------------");                        
    
                    }
    
                }
    
            }
    

    If you want to return all the properties to see what's available then just comment out the ds.PropertiesToLoad lines and that will give you the full list.