简体   繁体   English

检测我连接到的VPN

[英]Detect what VPN I'm connected to

I'm currently working on an desktop tool to automate VPN connections to customers. 我目前正在使用桌面工具来自动化与客户的VPN连接。 I'd like to have a good way to know for sure I'm indeed connected to a VPN. 我想有一个很好的方法来确定我确实已连接到VPN。 I'm currently thinking about using the routing table as it's shown in netstat-rn and comparing it to the IP I should be connected to. 我目前正在考虑使用netstat-rn中显示的路由表,并将其与我应该连接的IP进行比较。

My question is, how would I be able to get these IP's in my C# application, or is there a better way to determine if I'm connected to a VPN. 我的问题是,如何在我的C#应用​​程序中获得这些IP,或者是否有更好的方法确定我是否已连接到VPN。

Thanks in advance! 提前致谢!

This is so classic, I spend a couple of hours looking for the solution, and when I ask for help I find it myself. 这是如此经典,我花了几个小时寻找解决方案,当我寻求帮助时,我自己找到了解决方案。 For those who wonder, this as the solution. 对于那些想知道的人, 是解决方案。

try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_IP4RouteTable");
            ListViewItem buf;

            foreach (ManagementObject queryObj in searcher.Get())
            {
                string destination = queryObj["Destination"].ToString();
                string mask = queryObj["Mask"].ToString();
                string metric = queryObj["Metric1"].ToString();
                string interfaceIndex = queryObj["InterfaceIndex"].ToString();
                string nexthop = queryObj["NextHop"].ToString();
                string protocol =queryObj["Protocol"].ToString();
                string type = queryObj["Type"].ToString();
                string status;
                if (queryObj["Status"]!=null)
                {
                  status = queryObj["Status"].ToString();
                }
                else
                {
                   status = string.Empty;
                }


                buf = new ListViewItem(new string[] {destination,mask,metric,interfaceIndex,nexthop,protocol,status,typ});
                list_route.Items.Add(buf);

            }
        }
        catch (ManagementException ex)
        {
            MessageBox.Show("An error occurred while querying for WMI data: " + ex.Message);
        }

Anyway, thanks everyone who took the time to try and help me! 无论如何,感谢所有花时间尝试帮助我的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM