PriyankGhorecha-6516 avatar image
0 Votes"
PriyankGhorecha-6516 asked PriyankGhorecha-6516 commented

Identify IPaddress/Hostname is of a printer and not a computer C# WPF

I have developed a application where user can add new printer and perform task with it, when adding new printer user needs to give IP Address or Hostname. If valid IP Address or Hostname is entered and is online then device is added to application.
I am stuck at a point where if user enter IP Address or Hostname of a computer then also application is adding it as a printer. I want to implement functionality which check if IP Address or Hostname entered by user is of a computer then it will not notify user and if valid printer IP Address or Hostname is entered then it will added as printer in application.
I have searched multiple ways through which i can validate that IP Address or hostname given by user and conclude it is a valid printer or not.

Some of ways which i have tried are following

Used WIN32 class approach - Using this approach gives me list of printers which are installed on that respective machine.

 static void Main(string[] args)
         var scope = new ManagementScope(@"\root\cimv2");
         var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
         var results = searcher.Get();
         Console.WriteLine("Network printers list:");
         foreach (var printer in results)
             var portName = printer.Properties["PortName"].Value;
             var searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_TCPIPPrinterPort where Name LIKE '" + portName + "'");
             var results2 = searcher2.Get();
             foreach (var printer2 in results2)
                 Console.WriteLine("Name:" + printer.Properties["Name"].Value);
                 //Console.WriteLine("PortName:" + portName);
                 Console.WriteLine("PortNumber:" + printer2.Properties["PortNumber"].Value);
                 Console.WriteLine("HostAddress:" + printer2.Properties["HostAddress"].Value);

Directory search approach - In this approach I am not findings any result on my machine don't know reason.

 static void Main(string[] args)
     List<string> list = new List<string>();
     DirectorySearcher directorySearcher = new DirectorySearcher();
     directorySearcher.SearchRoot = new DirectoryEntry("");
     directorySearcher.Filter = "(|(&(objectCategory=printQueue)(name=" + hostName + "*)))";
     directorySearcher.Sort = new SortOption("name", SortDirection.Ascending);
         SearchResultCollection searchResultCollection = directorySearcher.FindAll();
         foreach (SearchResult item in searchResultCollection)
             IEnumerator enumerator2 = item.Properties.Values.GetEnumerator();
             foreach (string propertyName in item.Properties.PropertyNames)
                 if (propertyName.ToLower(CultureInfo.InvariantCulture).Contains("uncname"))
                     object[] array = new object[((ResultPropertyValueCollection)enumerator2.Current).Count];
                     ((ResultPropertyValueCollection)enumerator2.Current).CopyTo(array, 0);
         return list;
     catch (Exception ex)
         throw new InvalidOperationException(ex.Message);

I want to identify if device type like is it a computer or a printer based on IP Address or Hostname entered by user. Please suggest a way through which we can achieve this.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I think you can get all Addresses of printer using printer2.Properties["HostAddress"].Value.If your ip adderss is in these ,it is a printer.

0 Votes 0 ·

Thank you @AlexLi-MSFT for quickly replying, but it will give me printer which are already installed to my machine. I want to identify if IP Address or Hostname entered by user is a printer or a computer.

0 Votes 0 ·

0 Answers