直接通过内置API可以获取,下面代码直接复制使用。
// 获取系统的IP地址private string GetIP(){NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();foreach (NetworkInterface adater in adapters){if (adater.Supports(NetworkInterfaceComponent.IPv4)){UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses;if (UniCast.Count > 0){foreach (UnicastIPAddressInformation uni in UniCast){if (uni.Address.AddressFamily == AddressFamily.InterNetwork){return uni.Address.ToString();}}}}}return null;}