<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; local ip</title>
	<atom:link href="http://www.dijksterhuis.org/tag/local-ip/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Finding all local IP Addresses in C#</title>
		<link>http://www.dijksterhuis.org/finding-the-local-ip-addresses-in-c/</link>
		<comments>http://www.dijksterhuis.org/finding-the-local-ip-addresses-in-c/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 06:53:29 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[local ip]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=321</guid>
		<description><![CDATA[A common problem is the find out the IP address of the machine your C# program is running on. C# provides two methods for obtaining the information. The first one is easy; but somewhat undocumented. The other one is just a little harder. If your code has to run on Mono then the second option [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p>A common problem is the find out the IP address of the machine your C# program is running on. C# provides two methods for obtaining the information. The first one is easy; but somewhat undocumented. The other one is just a little harder. If your code has to run on Mono then the second option is your only alternative.</p>
<p><span id="more-321"></span></p>
<p>C# originally provided basic system information through the <em>System.Net.DNS</em> class. By querying the DNS &#8220;GetHostEntry&#8221; we could also discover the local IP addresses. The problem is that under Mono this only returns the local loop-back connector making this not a portable solution. </p>
<p>In .NET 2.0 the <em>System.Net.NetworkInformation</em> class was added, which allows you to browse all the network adapters and query their configuration in much greater detail. </p>
<p>The code below shows both methods for querying the local configuration: </p>
<pre class="brush: c#">
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Net.Sockets;

namespace IPLookup
{
    class MainClass
    {
        public static IPAddress[] GetAllUnicastAddresses_Old()
        {
            // By passing an empty string to GetHostEntry we receive all the IP addresses on the local machine
            // This breaks on Mono
            IPHostEntry LocalEntry = Dns.GetHostEntry(&quot;&quot;);
            return LocalEntry.AddressList;
        }

        public static IPAddress[] GetAllUnicastAddresses_New()
        {
            // This works on both Mono and .NET , but there is a difference: it also
            // includes the LocalLoopBack so we need to filter that one out
            List&lt;IPAddress&gt; Addresses = new List&lt;IPAddress&gt;();
            // Obtain a reference to all network interfaces in the machine
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                foreach (IPAddressInformation uniCast in properties.UnicastAddresses)
                {
                    // Ignore loop-back addresses &amp; IPv6
                    if (!IPAddress.IsLoopback(uniCast.Address) &amp;&amp; uniCast.Address.AddressFamily!= AddressFamily.InterNetworkV6)
                      Addresses.Add(uniCast.Address);
                }

            }
            return Addresses.ToArray();
        }

        public static void Main(string[] args)
        {
            IPAddress[] Adresses = GetAllUnicastAddresses_Old();
            foreach (IPAddress Adres in Adresses)
            {
                Console.WriteLine(&quot;OLD IP Address: {0}&quot;, Adres);
            }

            IPAddress[] Adresses2 = GetAllUnicastAddresses_New();
            foreach (IPAddress Adres in Adresses2)
            {
                Console.WriteLine(&quot;NEW IP Address: {0}&quot;, Adres);
            }
        }

    }
}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/finding-the-local-ip-addresses-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
