参考: How do I determine the local host’s IPv4 addresses? 传入你的VPN连接的名字,就可以获取它的IPV4地址using System; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text;namespace GetVPNIP { class Program { public static void Main(string[] args) { DisplayIPAddresses("你的VPN连接的名字"); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
/// <summary> /// This utility function displays all the IP (v4, not v6) addresses of the local computer. /// </summary> public static void DisplayIPAddresses(string name) { StringBuilder sb = new StringBuilder(); // Get a list of all network interfaces (usually one per network card, dialup, and VPN connection) NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface network in networkInterfaces) { if (network.Name == name) {
// Read the IP configuration for each network IPInterfaceProperties properties = network.GetIPProperties(); // Each network interface may have multiple IP addresses foreach (IPAddressInformation address in properties.UnicastAddresses) { // We're only interested in IPv4 addresses for now if (address.Address.AddressFamily != AddressFamily.InterNetwork) continue; // Ignore loopback addresses (e.g., 127.0.0.1) if (IPAddress.IsLoopback(address.Address)) continue; sb.AppendLine(address.Address.ToString() + " (" + network.Name + ")"); sb.AppendLine() } break; } } Console.WriteLine(sb.ToString()); } } }
foreach (RasConnection connection in RasConnection.GetActiveConnections()) { if (connection.EntryName == "KULENG-VPN-HK") {///判断VPN名称是否相同 RasIPInfo ipAddresses = (RasIPInfo)connection.GetProjectionInfo(RasProjectionType.IP); if (ipAddresses != null) { newaddressip = ipAddresses.IPAddress.ToString();
传入你的VPN连接的名字,就可以获取它的IPV4地址using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;namespace GetVPNIP
{
class Program
{
public static void Main(string[] args)
{
DisplayIPAddresses("你的VPN连接的名字");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
/// <summary>
/// This utility function displays all the IP (v4, not v6) addresses of the local computer.
/// </summary>
public static void DisplayIPAddresses(string name)
{
StringBuilder sb = new StringBuilder(); // Get a list of all network interfaces (usually one per network card, dialup, and VPN connection)
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface network in networkInterfaces)
{
if (network.Name == name) {
// Read the IP configuration for each network
IPInterfaceProperties properties = network.GetIPProperties(); // Each network interface may have multiple IP addresses
foreach (IPAddressInformation address in properties.UnicastAddresses)
{
// We're only interested in IPv4 addresses for now
if (address.Address.AddressFamily != AddressFamily.InterNetwork)
continue; // Ignore loopback addresses (e.g., 127.0.0.1)
if (IPAddress.IsLoopback(address.Address))
continue; sb.AppendLine(address.Address.ToString() + " (" + network.Name + ")");
sb.AppendLine()
}
break;
}
} Console.WriteLine(sb.ToString());
}
}
}
{
if (connection.EntryName == "KULENG-VPN-HK")
{///判断VPN名称是否相同
RasIPInfo ipAddresses = (RasIPInfo)connection.GetProjectionInfo(RasProjectionType.IP); if (ipAddresses != null)
{
newaddressip = ipAddresses.IPAddress.ToString();
// string s3 = ipAddresses.ServerIPAddress.ToString();
}
}
}我这里做了一个更简单的获取VPN分配IP的。大家的也可以学习,楼上的那种方法也是可以的
foreach (var item in networkInterfaces)
{
if (item.NetworkInterfaceType == System.Net.NetworkInformation.NetworkInterfaceType.Ppp)
{ }
}
六楼的代码改一下就可以了