Get connection status VPN using dotras

5.2k Views Asked by At

I write code to create and connect to VPN using Dotras in C#. It works very good, but when I write code to get connection status, It doesn't work. I read Dotras document, and write code like example, but It still doesn't work.

It doesn't show status in multiple line textbox. :(

Please show me, what I wrong. Thank you.

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DotRas;
using System.Net;

namespace VPN1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    private void btn_create_vpn_Click(object sender, EventArgs e)
    {
        try
        {
            string vpnuser = txt_vpn_user.Text;
            string ip_address = txt_IP.Text;
            this.rasPhoneBook1.Open();
            RasEntry entry = RasEntry.CreateVpnEntry(vpnuser, ip_address, RasVpnStrategy.Default, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn, false));
            this.rasPhoneBook1.Entries.Add(entry);
            MessageBox.Show("Success");
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }  
    }
    private RasHandle handle = null;
    private void btn_dial_Click(object sender, EventArgs e)
    {
        this.rasDialer1.EntryName = txt_vpn_user.Text;
        string username = txt_user.Text;
        string password = txt_pass.Text;
        this.rasDialer1.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
        try
        {
            this.rasDialer1.Credentials = new NetworkCredential(username, password);
            this.handle = this.rasDialer1.DialAsync();
            this.btn_disconnect.Enabled = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }

    private void rasDialer1_StateChanged(object sender, StateChangedEventArgs e)
    {
        this.txt_status.AppendText(e.State.ToString() + "\r\n");
    }

    private void rasDialer1_DialCompleted(object sender, DialCompletedEventArgs e)
    {
        if(e.Cancelled)
        {
            this.txt_status.AppendText("Cancelled");
        }
        else if(e.TimedOut)
        {
            this.txt_status.AppendText("Timeout");
        }
        else if(e.Connected)
        {
            this.txt_status.AppendText("Connection successful");

        }
        else if (e.Error != null)
        {
            this.txt_status.AppendText(e.Error.ToString());
        }
        if(!e.Connected)
        {
            this.btn_disconnect.Enabled = false;
        }
    }

    private void btn_disconnect_Click(object sender, EventArgs e)
    {
        if(this.rasDialer1.IsBusy)
        {
            this.rasDialer1.DialAsyncCancel();
        }
        else
        {
            RasConnection connection = RasConnection.GetActiveConnectionByHandle(this.handle);
            if(connection!=null)
            {
                connection.HangUp();
            }
        }
    }
}
}
3

There are 3 best solutions below

1
On

try:

    {

        File.WriteAllText("your rasphone.pbk  path","")//Add
        string vpnuser = txt_vpn_user.Text;
        string ip_address = txt_IP.Text;
        this.rasPhoneBook1.Open();
        RasEntry entry = RasEntry.CreateVpnEntry(vpnuser, ip_address, RasVpnStrategy.Default, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn, false));
        this.rasPhoneBook1.Entries.Add(entry);
        MessageBox.Show("Success");
    }
0
On

If the events are not firing, the issue might be that the Events are not set in the events section in the properties window of the rasDialer1 control you added to the form (Form1). Using Visual Studio, Enter the design view, click on the rasDialer1 control which reveals the properties of the RasDialer you added, then navigate to events section (marked with a lightening icon) and then set the StateChanged and DialCompleted events.

OR

You can do all this from code simply by

rasDialer.StateChanged += rasDialer1_StateChanged;
rasDialer.DialCompleted += rasDialer1_DialCompleted;

somewhere in the Form1() constructor, where rasDialer1_StateChanged and rasDialer1_DialCompleted are the event handlers in your code.

I'm also anticipating a potential error within your code where accessing UI controls in those event handlers will get you a Cross-thread operation not valid error since they are called from an async operation this.rasDialer1.DialAsync();

The right way of accessing controls from methods called from another thread is below ...

textbox1.Invoke(new Action(() => textbox1.text = "my string"));
0
On

Try using dial instead of dialAsync