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;
using
DotRas;
namespace
WindowsFormsApplication1
{
public
partial
class
Form1 : Form
{
public
RasPhoneBook book;
public
RasEntry entry;
public
DotRas.RasDialer dailer=
new
DotRas.RasDialer();
//稍后在断开连接时候需要用到
private
RasHandle Rashandler=
null
;
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
book =
new
DotRas.RasPhoneBook();
book.Open();
dailer.StateChanged +=
new
EventHandler(dailer_StateChanged);
dailer.DialCompleted +=
new
EventHandler(dailer_DialCompleted);
dailer.SynchronizingObject =
this
;
}
void
dailer_DialCompleted(
object
sender, DialCompletedEventArgs e)
{
if
(e.Cancelled)
{
this
.txtStatus.AppendText(
"Cancelled!"
);
}
else
if
(e.TimedOut)
{
this
.txtStatus.AppendText(
"Connection attempt timed out!"
);
}
else
if
(e.Error !=
null
)
{
this
.txtStatus.AppendText(e.Error.ToString());
}
else
if
(e.Connected)
{
this
.txtStatus.AppendText(
"Connection successful!"
);
}
if
(!e.Connected)
{
this
.btnDisconnect.Enabled =
false
;
}
else
{
this
.btnDisconnect.Enabled =
true
;
}
}
void
dailer_StateChanged(
object
sender, StateChangedEventArgs e)
{
this
.txtStatus.AppendText(e.State.ToString() +
"\r\n"
);
}
private
void
button1_Click(
object
sender, 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=
new
System.Net.NetworkCredential(
"username"
,
"password"
);
//异步进行拨号,并把RAS的handler传递给定义好的Rashandler变量,以便在断开连接中进行处理
Rashandler = dailer.DialAsync();
}
catch
(Exception ex)
{}
}
private
void
btnDisconnect_Click(
object
sender, EventArgs e)
{
if
(dailer.IsBusy)
{}
else
{
RasConnection connection=RasConnection.GetActiveConnectionByHandle(
this
.Rashandler);
if
(connection!=
null
)
{
connection.HangUp();
}
}
}
}
}
- c#实现建立拨号连接-PPOE,VPN(异步方式)
- vb建立 VPN 拨号连接
- vb建立 VPN 拨号连接
- Vista SP1 无法建立PPOE连接解决方法
- C#实现VPN连接
- ubuntu下建立PPTP-VPN客户端拨号连接 [已删除]
- ubuntu下建立PPTP-VPN客户端拨号连接
- ubuntu下建立PPTP-VPN客户端拨号连接(绝对可用)
- 济南广电无线路由PPoE拨号连接不上解决
- openwrt ppoe 拨号设置
- VPN 建立和连接
- C#建立VPN
- VPN连接:win7下建立VPN连接
- VPN连接:win7下建立VPN连接
- xp不能建立拨号连接
- 如何创建VPN的拨号连接
- VPN拨号连接报789错误解决方法
- ppoe拨号密码忘记找回方法
- Windows下XAMPP的配置
- POJ 1269 Intersecting Lines (两直线之间的位置关系)
- Android中asset文件夹和raw文件夹区别
- http://acm.nyist.net/JudgeOnline/problem.php?pid=431
- 如何编译动态链接库
- c#实现建立拨号连接-PPOE,VPN(异步方式)
- 机房收费系统测试
- MySQL 主从模式设置
- 时间简史
- PCB布局经验谈
- 編譯FastDFS
- 析构函数显示调用(系统会默认在调用一次)而构造函数不允许显示调用
- 一个摄像头的控制类
- 源代码管理工具GIT之使用