使用 C# 连接到永远在线的 VPN

Connect to Always-On VPN using C#

第一次写post,多多包涵。

我已经为我的工作制作了一个程序,现在被要求在其中实现一个功能,我们的用户可以使用该程序连接到我们的 VPN(永远在线的 VPN)。 但是,我似乎无法找到有关如何以编程方式实际建立连接的任何信息。我基本上需要能够点击一个显示“连接到 VPN”的按钮,然后它将建立连接,就像在 Windows 中点击连接时所做的那样:

Clicking Connect in Windows

我已经想出如何通过我的程序查看连接是否建立:

    public bool CheckForVPNInterface()
    {
        if (NetworkInterface.GetIsNetworkAvailable())
        {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface @interface in interfaces)
            {
                if (@interface.Description.Contains("Always-On VPN") && @interface.OperationalStatus == OperationalStatus.Up)
                {
                    Label_Connection.BackColor = Color.Green;
                    return true;
                }
                else
                    Label_Connection.BackColor = Color.Red;
            }
            
        }
        return false;    
    }

但现在只需要有“连接到 VPN”按钮即可。

知道怎么做吗?

注意:我试过使用 rasdial,但没用

我找不到关于这个主题的太多信息,但我发现这个晦涩 Windows API:https://docs.microsoft.com/en-us/previous-versions/windows/desktop/get_connected/getvpnconnected

编辑:maybe this will also help