C#实现ADSL断开链接或者拨号

来源:互联网 发布:淘宝我的购物车打不开 编辑:程序博客网 时间:2024/06/06 16:54

同理,需要换IP,如果是直接拨号上网的话,可以考虑使用这个方法来更换IP,不过效果不是特别好,网络运营商好多都是就给你分配那几个IP地址,用过了就没用了。。


将就用一下吧



首先需要引用 

using DotRas;
可以在NuGet里找到,根据系统版本来引用
我是Win10系统,用的Win8版本的 可用


ADSL类


代码:


using DotRas;using System.Collections.ObjectModel;using System.Linq;using System.Net;namespace Base{    /// <summary>    /// 断开    /// </summary>    public static class Adsl    {        /// <summary>        /// 创建或更新一个PPPOE连接(指定PPPOE名称)        /// </summary>        static void CreateOrUpdatePPPOE(string updatePPPOEname)        {            RasDialer dialer = new RasDialer();            RasPhoneBook allUsersPhoneBook = new RasPhoneBook();            string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);            allUsersPhoneBook.Open(path);            // 如果已经该名称的PPPOE已经存在,则更新这个PPPOE服务器地址            if (allUsersPhoneBook.Entries.Contains(updatePPPOEname))            {                allUsersPhoneBook.Entries[updatePPPOEname].PhoneNumber = " ";                // 不管当前PPPOE是否连接,服务器地址的更新总能成功,如果正在连接,则需要PPPOE重启后才能起作用                allUsersPhoneBook.Entries[updatePPPOEname].Update();            }            // 创建一个新PPPOE            else            {                string adds = string.Empty;                ReadOnlyCollection<RasDevice> readOnlyCollection = RasDevice.GetDevices();                //                foreach (var col in readOnlyCollection)                //                {                //                    adds += col.Name + ":" + col.DeviceType.ToString() + "|||";                //                }                //                _log.Info("Devices are : " + adds);                // Find the device that will be used to dial the connection.                RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First();                RasEntry entry = RasEntry.CreateBroadbandEntry(updatePPPOEname, device);    //建立宽带连接Entry                entry.PhoneNumber = " ";                allUsersPhoneBook.Entries.Add(entry);            }        }        /// <summary>        /// 断开 宽带连接        /// </summary>        public static void Disconnect()        {            ReadOnlyCollection<RasConnection> conList = RasConnection.GetActiveConnections();            foreach (RasConnection con in conList)            {                con.HangUp();            }        }        /// <summary>        /// 宽带连接,成功返回true,失败返回 false        /// </summary>        /// <param name="PPPOEname">宽带连接名称</param>        /// <param name="username">宽带账号</param>        /// <param name="password">宽带密码</param>        /// <returns></returns>        public static bool Connect(string PPPOEname, string username, string password, ref string msg)        {            try            {                CreateOrUpdatePPPOE(PPPOEname);                using (RasDialer dialer = new RasDialer())                {                    dialer.EntryName = PPPOEname;                    dialer.AllowUseStoredCredentials = true;                    dialer.Timeout = 1000;                    dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);                    dialer.Credentials = new NetworkCredential(username, password);                    dialer.Dial();                    return true;                }            }            catch (RasException re)            {                msg = re.ErrorCode + " " + re.Message;                return false;            }        }    }}


原创粉丝点击