c#实现建立拨号连接-PPOE,VPN(异步方式)

来源:互联网 发布:mysql 阻塞 编辑:程序博客网 时间:2024/04/30 10:16
 

之前有过一篇关于DOTRAS的帖子,不过那个是同步连接的,这个是异步连接,没测试好不好,先转来

原贴:http://www.linkanyway.com/post/2010/12/11/dotrasproject.aspx

在上一篇文章中曾经提到过pbk这个文件对于系统拨号连接的重要性,并且提出可以用vbs等脚本或者任何程序方式通过按标准格式建立pbk文件并调用系统rasdial命令来完成建立连接和拨号的动作。

但是这样做限制很多又麻烦,而且无法准确捕捉系统拨号的情况。因此我觉得非常有必要google下c#下的实现方式,win32 api这种东西对于我来说太难,因此我把搜索范围定在C#本身含有的库中,搜了半天没有任何结果,就在快失望的时候我搜到了一根救命稻草DotRas初步看了下文档完全满足我的需要。
具体看官还是自己去看下文档这里只贴出我测试用的基础代码。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotRas.Internal;
usingDotRas;
namespaceWindowsFormsApplication1
{
    publicpartial classForm1 : Form
    {
        publicRasPhoneBook book;
        publicRasEntry entry;
        publicDotRas.RasDialer dailer=newDotRas.RasDialer();
        //稍后在断开连接时候需要用到
        privateRasHandle Rashandler=null;
        publicForm1()
        {
            InitializeComponent();
        }
  
        privatevoid Form1_Load(objectsender, EventArgs e)
        {
             book =new DotRas.RasPhoneBook();
            book.Open();
         
            dailer.StateChanged +=new EventHandler(dailer_StateChanged);
            dailer.DialCompleted +=new EventHandler(dailer_DialCompleted);
            dailer.SynchronizingObject =this;
  
        }
  
        voiddailer_DialCompleted(objectsender, DialCompletedEventArgs e)
        {
            if(e.Cancelled)
            {
                this.txtStatus.AppendText("Cancelled!");
            }
            elseif (e.TimedOut)
            {
                this.txtStatus.AppendText("Connection attempt timed out!");
            }
            elseif (e.Error != null)
            {
                this.txtStatus.AppendText(e.Error.ToString());
            }
            elseif (e.Connected)
            {
                this.txtStatus.AppendText("Connection successful!");
            }
  
            if(!e.Connected)
            {
                this.btnDisconnect.Enabled =false;
            }
            else
            {
                this.btnDisconnect.Enabled =true;
            }
        }
  
        voiddailer_StateChanged(objectsender, StateChangedEventArgs e)
        {
            this.txtStatus.AppendText(e.State.ToString() +"\r\n");
        }
  
        privatevoid button1_Click(objectsender, EventArgs e)
        {
            //假设8.8.8.8为远程VPN服务器地址,vpn类型为pptp
            entry = RasEntry.CreateVpnEntry("VpnCreateByC#","8.8.8.8", RasVpnStrategy.PptpOnly, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));
            book.Entries.Add(entry);
            txtStatus.Clear();
            //示例程序不做vpn连接已存在的exception处理
            dailer.EntryName="VpnCreateByC#";
            dailer.PhoneBookPath=RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
          try
          {
          dailer.Credentials=newSystem.Net.NetworkCredential("username","password");
       //异步进行拨号,并把RAS的handler传递给定义好的Rashandler变量,以便在断开连接中进行处理
              Rashandler = dailer.DialAsync();
          }
            catch(Exception ex)
          {}
        }
  
        privatevoid btnDisconnect_Click(objectsender, EventArgs e)
        {
            if(dailer.IsBusy)
            {}
            else
            {
            RasConnection connection=RasConnection.GetActiveConnectionByHandle(this.Rashandler);
              if(connection!=null)
              {
              connection.HangUp();
              }
  
            }
        }
    }
}
原创粉丝点击