有些场合,为了避免服务对用户IP的限制或者为了用户的方便,可以通过代码实现自动化的拨号或者断网操作,通过DotRAS组件,可以非常方便的实现如ADSL、VPN等拨号以及相关操作,DotRAS组件是专门提供这样远程访问服务的模块,本文介绍如何通过应用该组件,实现ADSL网络的拨号、断网、获取用户IP的操作。
DotRAS组件的项目地址是:http://dotras.codeplex.com/
先看看Demo的界面效果
具体的代码逻辑,是通过列出电话簿里面的拨号连接,设置是通过账户密码或者默认账户设置信息,进行拨号即可,使用DotRas组件,使得在DotNet中操作这些功能非常方便,代码贴上如下所示:
///
<summary>
/// 测试拨号连接
/// </summary>
private void btnTest_Click( object sender, EventArgs e)
{
try
{
RasDialer dialer = new RasDialer();
dialer.EntryName = " 联通网络 " ;
dialer.PhoneNumber = " " ;
dialer.AllowUseStoredCredentials = true ;
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
dialer.Timeout = 1000 ;
dialer.Dial();
Thread.Sleep( 100 );
this .LoadConnections();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 断开网络连接
/// </summary>
private void btnLogout_Click( object sender, EventArgs e)
{
ReadOnlyCollection < RasConnection > conList = RasConnection.GetActiveConnections();
foreach (RasConnection con in conList)
{
con.HangUp();
}
this .LoadConnections();
}
private void Form1_Load( object sender, EventArgs e)
{
this .LoadConnections();
}
/// <summary>
/// 显示活动的连接
/// </summary>
private void LoadConnections()
{
this .comboBox1.Items.Clear();
this .comboBox1.Items.Add( new ComboBoxItem( " 请选择一个链接... " , null ));
foreach (RasConnection connection in RasConnection.GetActiveConnections())
{
this .comboBox1.Items.Add( new ComboBoxItem(connection.EntryName, connection.EntryId));
}
this .comboBox1.SelectedIndex = 0 ;
}
private void comboBox1_SelectedIndexChanged( object sender, EventArgs e)
{
this .GetAddressButton.Enabled = this .comboBox1.SelectedIndex > 0 ;
}
/// <summary>
/// 获取IP地址信息
/// </summary>
private void GetAddressButton_Click( object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (RasConnection connection in RasConnection.GetActiveConnections())
{
if (connection.EntryId == (Guid)((ComboBoxItem) this .comboBox1.SelectedItem).Value)
{
RasIPInfo ipAddresses = (RasIPInfo)connection.GetProjectionInfo(RasProjectionType.IP);
if (ipAddresses != null )
{
sb.AppendFormat( " ClientIP:{0}\r\n " , ipAddresses.IPAddress.ToString());
sb.AppendFormat( " ServerIP:{0}\r\n " , ipAddresses.ServerIPAddress.ToString());
}
}
sb.AppendLine();
}
MessageBox.Show(sb.ToString());
}
/// 测试拨号连接
/// </summary>
private void btnTest_Click( object sender, EventArgs e)
{
try
{
RasDialer dialer = new RasDialer();
dialer.EntryName = " 联通网络 " ;
dialer.PhoneNumber = " " ;
dialer.AllowUseStoredCredentials = true ;
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
dialer.Timeout = 1000 ;
dialer.Dial();
Thread.Sleep( 100 );
this .LoadConnections();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 断开网络连接
/// </summary>
private void btnLogout_Click( object sender, EventArgs e)
{
ReadOnlyCollection < RasConnection > conList = RasConnection.GetActiveConnections();
foreach (RasConnection con in conList)
{
con.HangUp();
}
this .LoadConnections();
}
private void Form1_Load( object sender, EventArgs e)
{
this .LoadConnections();
}
/// <summary>
/// 显示活动的连接
/// </summary>
private void LoadConnections()
{
this .comboBox1.Items.Clear();
this .comboBox1.Items.Add( new ComboBoxItem( " 请选择一个链接... " , null ));
foreach (RasConnection connection in RasConnection.GetActiveConnections())
{
this .comboBox1.Items.Add( new ComboBoxItem(connection.EntryName, connection.EntryId));
}
this .comboBox1.SelectedIndex = 0 ;
}
private void comboBox1_SelectedIndexChanged( object sender, EventArgs e)
{
this .GetAddressButton.Enabled = this .comboBox1.SelectedIndex > 0 ;
}
/// <summary>
/// 获取IP地址信息
/// </summary>
private void GetAddressButton_Click( object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (RasConnection connection in RasConnection.GetActiveConnections())
{
if (connection.EntryId == (Guid)((ComboBoxItem) this .comboBox1.SelectedItem).Value)
{
RasIPInfo ipAddresses = (RasIPInfo)connection.GetProjectionInfo(RasProjectionType.IP);
if (ipAddresses != null )
{
sb.AppendFormat( " ClientIP:{0}\r\n " , ipAddresses.IPAddress.ToString());
sb.AppendFormat( " ServerIP:{0}\r\n " , ipAddresses.ServerIPAddress.ToString());
}
}
sb.AppendLine();
}
MessageBox.Show(sb.ToString());
}
通过以上的代码,可以非常方便实现宽带的拨号连接和获取IP等设置,不过断网之后,一般的IP还是和原来一样,这个可能和服务器的缓存有关系,为了实现拨号后,本地为不同的IP设置,需要特别的处理才可以。