This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to get my own IP address in C#?
I need to get the IP address of the system, where the application is running by C# code
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress theaddress in ip)
{
String _ipAddress = theaddress.ToString();
}
I am using this code, but this is giving a different result in a different operating system. For example, in Windows 7 it is giving "fe80::e3:148d:6e5b:bcaa%14"
and Windows XP it is giving "192.168.10.93".
Note that you may have multiple IP addresses assigned to a machine. You can retrieve them like so (note: this code ignores the loopback address):
var iplist = new List<string>();
foreach (var iface in NetworkInterface.GetAllNetworkInterfaces())
{
var ips = iface.GetIPProperties().UnicastAddresses;
foreach (var ip in ips)
if (ip.Address.AddressFamily == AddressFamily.InterNetwork &&
ip.Address.ToString() != "127.0.0.1")
iplist.Add(ip.Address.ToString());
}
Namespaces used include:
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
Here you go - quick google:
http://msdn.microsoft.com/en-us/library/system.net.ipaddress(v=VS.100).aspx
Related
I am using xamarin forms and i have one problem,
i am trying to get the DNS server Adresse from WIFI, and a im using below code :
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
if (networkInterface.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
foreach (IPAddress dnsAdress in dnsAddresses)
{
Debug.WriteLine(dnsAdress);
}
}
}
The problem is when i use this code on Microsoft Emulator "UWP" everything work fine see picture bellow using UWP device
but nothing work when i use my android mobile see picture bellow
using android device
NB: all devices are connect on the same wifi
I had done a simple to test your code and got the same null. And then I read the IPInterfaceProperties.DnsAddresses Property document, it seems that it doesn't support the xamarin.android.
You can check the it:https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ipinterfaceproperties.dnsaddresses?view=net-6.0
In addition, I also search this but can not find any solution. So I try to use the android native method to get the dns address with the following code. So you can try to use the dependency service to get the dns address for android.
ConnectivityManager cm = (ConnectivityManager)GetSystemService(ConnectivityService);
//NetworkInfo has been deprecated, but I can not find the new api to to this
NetworkInfo activeNetworkInfo = cm.ActiveNetworkInfo;
foreach (Network network in cm.GetAllNetworks())
{
NetworkInfo networkInfo = cm.GetNetworkInfo(network);
if (networkInfo.GetType() == activeNetworkInfo.GetType())
{
LinkProperties lp = cm.GetLinkProperties(network);
foreach (InetAddress addr in lp.DnsServers)
{
var a = addr.GetAddress();
}
}
}
I'm using dns to ip script but it always give me the old ip ( maybe it using cache or something like that ) how i can get the new dns ip ?
var ips = System.Net.Dns.GetHostEntry("mann.chickenkiller.com").AddressList;
foreach (var ip in ips)
{
MessageBox.Show(ip.ToString());
}
I'm trying to build C# app (desktop,windows, it doesn't really matter for now), with which I would like to connect to my windows phone, using sockets, to transfer some data... I know how this can be achieved,.
When connecting through sockets I don't want to manually enter windows phone device's IP address. So I want to know if it is possible to send some HTTP request from Windows phone app with some message, and fetch that message on computer, to be sure which IP is windows phone's IP?
In other words how to know which IP address belongs to Windows phone's IP address from Bunch of Ip addresses of devices on network?
Fallow this Link from Here
Add NameSpace :
using Windows.Networking;
public static string FindIPAddress()
{
List<string> ipAddresses = new List<string>();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
//IanaInterfaceType == 71 => Wifi
//IanaInterfaceType == 6 => Ethernet (Emulator)
if (hn.IPInformation != null &&
(hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71
|| hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
{
string ipAddress = hn.DisplayName;
ipAddresses.Add(ipAddress);
}
}
if (ipAddresses.Count < 1)
{
return null;
}
else if (ipAddresses.Count == 1)
{
return ipAddresses[0];
}
else
{
//if multiple suitable address were found use the last one
//(regularly the external interface of an emulated device)
return ipAddresses[ipAddresses.Count - 1];
}
}
Edit:
If I am wrong You want to get particular Windows Phone IP address from bunch of IP addresses. AFAIK you can't get like this. But I suggest you to Append some string when getting IP of Windows Phone like below which will vary from other Ip addresses by looking it self.
string IPadd = FindIPAddress();
string WPIPadd = IPadd + " - Windows phone "
MessageDialog.show(WPIPadd);
If you find any thing new apart from this Let us know. Thanks
I am building a SMTP diagnostics tool by using c# 4.0
If I know the IP Address of Primary NS of a domain, I can get MX,A and CNAME records.
So I can validate any email and run legal diagnostics commands. , if I can connect to mail server.
My problem is that I could not find a proper .NET solution to get Primary NS for a given domain.
I know there are some managed clients but I can not add them as reference to my solution or their source code is closed.
What is the differences of managed code and .NET for this issue , that managed code can query NS of a domain and .NET can not as stated here. ?
What is the proper way to implement such a kind of functionality ?
Regards
You can get the IP of your DNS server using IPInterfaceProperties.DnsAddresses. A code example can be found here: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipinterfaceproperties.dnsaddresses.aspx
You can then query that server using the component found here: http://www.codeproject.com/Articles/12072/C-NET-DNS-query-component
You can find the primary DNS server by querying for SOA records.
List<IPAddress> dnsServers = new List<IPAddress>();
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
IPAddressCollection adapterDnsServers = adapterProperties.DnsAddresses;
if (adapterDnsServers.Count > 0)
dnsServers.AddRange(adapterDnsServers);
}
foreach (IPAddress dnsServer in (from d in dnsServers
where d.AddressFamily == AddressFamily.InterNetwork
select d))
{
Console.WriteLine("Using server {0}", dnsServer);
// create a request
Request request = new Request();
// add the question
request.AddQuestion(new Question("stackoverflow.com", DnsType.MX, DnsClass.IN));
// send the query and collect the response
Response response = Resolver.Lookup(request, dnsServer);
// iterate through all the answers and display the output
foreach (Answer answer in response.Answers)
{
MXRecord record = (MXRecord)answer.Record;
Console.WriteLine("{0} ({1}), preference {2}", record.DomainName, Dns.GetHostAddresses(record.DomainName)[0], record.Preference);
}
Console.WriteLine();
}
Console.ReadLine();
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
how to get my own IP address in C#?
how to get ip address of machine in c#
Hi all, I am currently developing a c# application for windows using WPF. I would like to get the computers external IP address i.e. the internet address not the local computer ip address or the local router address.
Thanks for your help.
Like stated earlier, you need an external web server. An easy call to HTTP GET with the URL "http://checkip.dyndns.org/" will get you a simple text string with your IP.
You need to have a web server sitting somewhere in the cloud so that you can call and that will be able to give you your external IP address.
Looks like this one is free.
[EDIT]
A simple request to here will get you your ip.
This is a way to get any network address (not necessarily the internet ip) as pointed out in the comments:
IPAddress host = IPAddress.None;
foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
{
host = ip;
if (ip.AddressFamily == AddressFamily.InterNetwork)
break;
}
The only way I have found is to do a httpWebRequest to http://www.whatismyip.com/automation/n09230945.asp and parse the results for the ip
You can try connecting to whatismyip.com, as shown in the code below:
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace DreamInCode.Snippets
{
public class IpFinder
{
public static IPAddress GetExternalIp()
{
string whatIsMyIp = "http://whatismyip.com";
string getIpRegex = #"(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)";
WebClient wc = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
string requestHtml = "";
try
{
requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
}
catch (WebException we)
{
// do something with exception
Console.Write(we.ToString());
}
Regex r = new Regex(getIpRegex);
Match m = r.Match(requestHtml);
IPAddress externalIp = null;
if (m.Success)
{
externalIp = IPAddress.Parse(m.Value);
}
return externalIp;
}
}
}
NOTE: The code comes from this post http://www.dreamincode.net/forums/topic/24692-showing-the-external-ip-address-in-c%23/ .