C# 利用DotRas 操作adsl
生活随笔
收集整理的這篇文章主要介紹了
C# 利用DotRas 操作adsl
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
開源項(xiàng)目地址:http://dotras.codeplex.com/
?
使用這個(gè)可以方便的操作ADSL撥號(hào)、斷開。有詳細(xì)的開發(fā)文檔,需要的可以自己去看。。
?
?
/// <summary>/// 創(chuàng)建或更新一個(gè)PPPOE連接(指定PPPOE名稱)/// </summary>void CreateOrUpdatePPPOE(string updatePPPOEname){RasDialer dialer = new RasDialer();RasPhoneBook allUsersPhoneBook = new RasPhoneBook();string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);allUsersPhoneBook.Open(path);// 如果已經(jīng)該名稱的PPPOE已經(jīng)存在,則更新這個(gè)PPPOE服務(wù)器地址if (allUsersPhoneBook.Entries.Contains(updatePPPOEname)){allUsersPhoneBook.Entries[updatePPPOEname].PhoneNumber = " ";// 不管當(dāng)前PPPOE是否連接,服務(wù)器地址的更新總能成功,如果正在連接,則需要PPPOE重啟后才能起作用 allUsersPhoneBook.Entries[updatePPPOEname].Update();}// 創(chuàng)建一個(gè)新PPPOEelse{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); //建立寬帶連接Entryentry.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">寬帶賬號(hào)</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;}}}?
轉(zhuǎn)載于:https://www.cnblogs.com/cyberarmy/p/5428055.html
總結(jié)
以上是生活随笔為你收集整理的C# 利用DotRas 操作adsl的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云sql监控配置-druid
- 下一篇: 成都欢乐谷退票需要多久