質問

How can i get the ip from a PPP or PPPT vpn connection?

I know the name of the VPN connection, so how can i filter

var nics = NetworkInterface.GetAllNetworkInterfaces();

to only get the vpn interface with the name "VPNConnection", and get the given ip address?

役に立ちましたか?

解決

Perhaps:

using System.Linq;

var vpn = NetworkInterface.GetAllNetworkInterfaces()
                          .First(x => x.Name == "VPNConnection");

EDIT: Whoops I didn't finish:

var ip = vpn.GetIPProperties().UnicastAddresses.First(x => x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).Address.ToString();

他のヒント

I've made an application for this case. Using the batch file with following command, I made a process that executes the .bat file and stores the ip in variable ip

            //make a batch file and write this in it
        File.WriteAllText(FolderPath + "\\getip.bat", "@echo off \r\n FOR /F \"tokens=1-6 delims=:. \" %%a in ('netsh int ip show address \"SanadConnectVPN\" ^|find \"IP Address\"') do set IP=%%c.%%d.%%e.%%f \r\n echo %IP%");

        var newProcess = new Process
        {
            StartInfo =
            {
                FileName = FolderPath + "\\getip.bat",
                WindowStyle = ProcessWindowStyle.Normal
            }
        };
        newProcess.StartInfo.RedirectStandardOutput = true;
        newProcess.StartInfo.UseShellExecute = false;
        newProcess.Start();
        string ip = newProcess.StandardOutput.ReadToEnd();
        newProcess.WaitForExit();
        MessageBox.Show(ip);
        return ip
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top