maartenvdv
maartenvdv

Reputation: 177

Get result after connecting to a VPN

I can connect to an existing VPN created with the following code.

string args = string.Format("{0} {1} {2}", connVpnName, connUserName, connPassWord);
var proc = new System.Diagnostics.Process
{
    StartInfo = new System.Diagnostics.ProcessStartInfo
    {
        FileName = "C:\\WINDOWS\\system32\\rasdial.exe",
        Arguments = args,
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};
proc.Start();
string output = "";
while (!proc.StandardOutput.EndOfStream)
{
    output += proc.StandardOutput.ReadLine();
    txtOutput.Text += proc.StandardOutput.ReadLine();
}

I need to know if the connection succeeded, the wrong credentials where used, the VPN doesn't excist or if the ip isn't online.

My first idea was to get the output from the commandprompt and search for keywords like "is connected". The problem with this method is that my users use multiple languages and the keywords will be different.

Can I use an other method to achieve this?

Upvotes: 3

Views: 1097

Answers (2)

Hamid Pourjam
Hamid Pourjam

Reputation: 20764

use DotRAS this library is a wrapper around windows api. Almost all functions are documented in MSDN.

Upvotes: 1

maartenvdv
maartenvdv

Reputation: 177

I have found the answer:

            proc.Start();

            //het resultaat bekijken
            proc.WaitForExit();
            switch (proc.ExitCode)
            {
                case 0: //connection succeeded
                    MessageBox.Show("Je bent geconnecteerd met de VPN");
                    isConnectedVPN = true;
                    btnConnectToVPN.Text = "Stop VPN";
                    break;
                case 691: //wrong credentials
                    MessageBox.Show("De username en/of het wachtwoord kloppen niet.");
                    break;
                case 623: // The VPN doesn't excist
                    MessageBox.Show("Deze VPN staat niet tussen de bestaande VPN's.");
                    break;
                case 868: //the IP or domainname can't be found
                    MessageBox.Show("Het ip-adres of de domeinnaam kan niet gevonden worden");
                    break;
                default: //other faults
                    MessageBox.Show("fout " + proc.ExitCode.ToString());
                    break;
            }

Upvotes: 0

Related Questions