检测我连接的 VPN

Detect what VPN I'm connected to

我目前正在开发一个桌面工具来自动连接到客户的 VPN。我想有一个好方法来确定我确实连接到 VPN。我目前正在考虑使用 netstat-rn 中显示的路由 table 并将其与我应该连接的 IP 进行比较。

我的问题是,我如何才能在我的 C# 应用程序中获取这些 IP,或者是否有更好的方法来确定我是否已连接到 VPN。

提前致谢!

这太经典了,我花了几个小时寻找解决方案,当我寻求帮助时,我自己找到了。 对于那些想知道的人,this 作为解决方案。

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);
        }

无论如何,感谢所有花时间帮助我的人!