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

  • Question

  • 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. Is it possible?

    Wednesday, March 25, 2020 9:22 AM

All replies

  • Hi Priyank36,
    I viewed your description, you can try to  get all the information of every printer by using System.Management.
    Here are some related threads you can refer to.
    [Identify connected network printers by its IP address]
    [Determine the IP Address of a Printer in C#]
    [How to get Printer Info in .NET?]
    Hope these are helpful to you.
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 26, 2020 9:42 AM
  • Maybe you can try WNetEnumResource or WNetGetResourceInformation




    Indicates the resource type. This can be one of the following values.

    Value Meaning
    The resource is a shared disk volume.
    The resource is a shared printer.
    The resource matches more than one type, for example, a container of both print and disk resources, or a resource which is neither print or disk.

    • Edited by Castorix31 Thursday, March 26, 2020 10:01 AM
    Thursday, March 26, 2020 9:57 AM
  • Hi Daniel_Zhang-MSFT

    Thank you for reply, I have gone through links you shared and tried solution mentioned. I found that result I am getting are all Installed Printers to my machine.

    My main aim is to verify if IP Address or Hostname entered by user is a printer or not, I only want to add IP Address or Hostname only if it is a printer else not. Please share your view or guidance to achieve this target.

    Thursday, March 26, 2020 1:37 PM