控制台程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections.Specialized;
namespace Test
{
class Program
{
static void Main(string[] args)
{
foreach (string IPv4 in GetLocalIPv4())
{
Console.WriteLine(IPv4);
}
Console.Read();
}
static string[] GetLocalIPv4()
{
StringCollection IPCollection = new StringCollection();
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress IPv4 in localIPs)
{
if (IPv4.AddressFamily == AddressFamily.InterNetwork)
{
IPCollection.Add(IPv4.ToString());
}
}
string[] IPArray = new string[IPCollection.Count];
IPCollection.CopyTo(IPArray, 0);
return IPArray;
}
}
}