0

我使用 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();
            }
        }
    }
}
}
4

3 回答 3

1

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

或者

您可以简单地通过代码完成所有这些操作

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"));
于 2017-12-18T12:22:11.623 回答
0

尝试使用 dial 而不是 dialAsync

于 2016-07-08T11:52:48.680 回答
-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");
    }
于 2016-12-22T07:34:04.520 回答