c# 4.0 - Get the server name and ip address in C# 2010 -
get server name , ip address in c# 2010
i want ip address of server. following code comes from:
public static void dogethostentry(string hostname) { iphostentry host; host = dns.gethostentry(hostname); messagebox.show("gethostentry({0}) returns:"+ hostname); foreach (ipaddress ip in host.addresslist) { messagebox.show(" {0}"+ ip.tostring()); } }
this code must know name of server computer.
addressfamily in system.net.ipaddress
system.net.ipaddress i;
string hostname = i.addressfamily.tostring();
error ------------->use of unassigned local variable 'i'
how can name of server computer?
public string[] servername() { string[] strip = displayipaddresses(); int countip = 0; (int = 0; < strip.length; i++) { if (strip[i] != null) countip++; } string[] name = new string[countip]; (int = 0; < strip.length; i++) { if (strip[i] != null) { try { name[i] = system.net.dns.gethostentry(strip[i]).hostname; } catch { continue; } } } return name; } public string[] displayipaddresses() { stringbuilder sb = new stringbuilder(); // list of network interfaces (usually 1 per network card, dialup, , vpn connection) networkinterface[] networkinterfaces = networkinterface.getallnetworkinterfaces(); int = -1; string[] s = new string[networkinterfaces.length]; foreach (networkinterface network in networkinterfaces) { i++; if (network.operationalstatus == operationalstatus.up) { if (network.networkinterfacetype == networkinterfacetype.tunnel) continue; if (network.networkinterfacetype == networkinterfacetype.tunnel) continue; //gatewayipaddressinformationcollection gate = network.getipproperties().gatewayaddresses; // read ip configuration each network ipinterfaceproperties properties = network.getipproperties(); //discard not have real gateaway if (properties.gatewayaddresses.count > 0) { bool = false; foreach (gatewayipaddressinformation ginfo in properties.gatewayaddresses) { //not true gateaway (vmware lan) if (!ginfo.address.tostring().equals("0.0.0.0")) { s[i] = ginfo.address.tostring(); = true; break; } } if (!good) { continue; } } else { continue; } } } return s; }
Comments
Post a Comment