使用dotras获取连接状态VPN

时间:2016-03-25 03:05:18

标签: c# vpn dotras

我编写代码来使用C#中的Dotras创建并连接到VPN。它工作得很好,但是当我编写代码来获取连接状态时,它不起作用。 我阅读了Dotras文档,并编写了类似示例的代码,但它仍然不起作用。

它不会在多行文本框中显示状态。 :(

请告诉我,我错了。谢谢。

这是我的代码:

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 个答案:

答案 0 :(得分:1)

如果事件未触发,则问题可能是事件未在您添加到表单(Form1)的rasDialer1控件的属性窗口的事件部分中设置。使用Visual Studio,进入设计视图,单击rasDialer1控件,该控件显示您添加的RasDialer的属性,然后导航到events部分(标有闪电图标),然后设置StateChanged和DialCompleted事件。

OR

您可以通过

从代码中完成所有这些操作
rasDialer.StateChanged += rasDialer1_StateChanged;
rasDialer.DialCompleted += rasDialer1_DialCompleted;

在Form1()构造函数中的某处,其中rasDialer1_StateChanged和rasDialer1_DialCompleted是代码中的事件处理程序。

我还预计您的代码中存在潜在错误,因为访问这些事件处理程序中的UI控件会导致Cross-thread operation not valid错误,因为它们是从异步操作调用的this.rasDialer1.DialAsync();

从另一个线程调用的方法访问控件的正确方法是......

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

答案 1 :(得分:0)

尝试使用dial而不是dialAsync

答案 2 :(得分:-1)

尝试:

    {

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