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

  • Thread starter Thread starter Priyank36
  • Start date Start date
P

Priyank36

Guest
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");
scope.Connect();
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);
}
Console.WriteLine();
}
Console.ReadLine();
}

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);
try
{
SearchResultCollection searchResultCollection = directorySearcher.FindAll();
foreach (SearchResult item in searchResultCollection)
{
IEnumerator enumerator2 = item.Properties.Values.GetEnumerator();
foreach (string propertyName in item.Properties.PropertyNames)
{
enumerator2.MoveNext();
if (propertyName.ToLower(CultureInfo.InvariantCulture).Contains("uncname"))
{
object[] array = new object[((ResultPropertyValueCollection)enumerator2.Current).Count];
((ResultPropertyValueCollection)enumerator2.Current).CopyTo(array, 0);
list.Add((string)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?

Continue reading...
 
Back
Top