開源項目地址:http://dotras.codeplex.com/
使用這個可以方便的操作ADSL撥號、斷開。有詳細的開發文檔,需要的可以自己去看。。
/// <summary> /// 創建或更新一個PPPOE連接(指定PPPOE名稱) /// </summary> 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 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 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; } } }