利用DotRAS组件,实现ADSL的自动拨号断网自动化操作



有些场合,为了避免服务对用户IP的限制或者为了用户的方便,可以通过代码实现自动化的拨号或者断网操作,通过DotRAS组件,可以非常方便的实现如ADSL、VPN等拨号以及相关操作,DotRAS组件是专门提供这样远程访问服务的模块,本文介绍如何通过应用该组件,实现ADSL网络的拨号、断网、获取用户IP的操作。

DotRAS组件的项目地址是:http://dotras.codeplex.com/  

先看看Demo的界面效果

 

具体的代码逻辑,是通过列出电话簿里面的拨号连接,设置是通过账户密码或者默认账户设置信息,进行拨号即可,使用DotRas组件,使得在DotNet中操作这些功能非常方便,代码贴上如下所示:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        /// <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());
        }

通过以上的代码,可以非常方便实现宽带的拨号连接和获取IP等设置,不过断网之后,一般的IP还是和原来一样,这个可能和服务器的缓存有关系,为了实现拨号后,本地为不同的IP设置,需要特别的处理才可以。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值