c# - Get local IP address -
in internet there several places show how ip address. , lot of them example:
string strhostname = string.empty; // getting ip address of local machine... // first host name of local machine. strhostname = dns.gethostname(); console.writeline("local machine's host name: " + strhostname); // using host name, ip address list.. iphostentry ipentry = dns.gethostentry(strhostname); ipaddress[] addr = ipentry.addresslist; (int = 0; < addr.length; i++) { console.writeline("ip address {0}: {1} ", i, addr[i].tostring()); } console.readline();
with example several ip addresses, i'm interested in getting 1 router assigns computer running program: ip give if wishes access shared folder in computer instance.
if not connected network , connected internet directly via modem no router error. how can see if computer connected network c# , if lan ip address.
to local ip address:
public static string getlocalipaddress() { var host = dns.gethostentry(dns.gethostname()); foreach (var ip in host.addresslist) { if (ip.addressfamily == addressfamily.internetwork) { return ip.tostring(); } } throw new exception("local ip address not found!"); }
to check if you're connected or not:
system.net.networkinformation.networkinterface.getisnetworkavailable();
Comments
Post a Comment