﻿# 如何用 C# 取得本機所有的 IP 位址

```csharp
// 取得本機名稱
String strHostName = Dns.GetHostName();

// 取得本機的 IpHostEntry 類別實體
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

// 取得所有 IP 位址
int num = 1;
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
     Console.WriteLine("IP #" + num + ": " + ipaddress.ToString());
     num = num + 1;
}
```

**執行結果：**

> Host Name: mypcname  
> IP #1: 192.168.2.19  
> IP #2: 192.168.238.1

備註：記得載入 System.Net 命名空間。
