C# 實現自動ADSL撥號
阿新 • • 發佈:2019-01-02
前言
由於工作原因,需要使電腦長時間保持寬頻連線狀態,並且有動態撥號更換ip的需求,在網上查閱資料許久,首先找到了這篇部落格:
使用C#實現ADSL自動撥號
但是本人在win8 64位的系統上並未成功,後來採用bat去實現這個功能並封裝成幫助類
類程式碼
類程式碼如下
//---------------------------------------名稱:基於bat批處理的ADSL動態撥號幫助類
//---------------------------------------更新時間:2017/10/18
//---------------------------------------作者:獻醜
//---------------------------------------CSDN地址:http://blog.csdn.net/qq_26712977
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
namespace HttpToolsLib
{
/// <summary>
/// ADSL撥號幫助類 用批處理實現
/// </summary>
public class ADSLIP
{
#region 變數
/// <summary>
///生成的臨時批處理檔名稱
/// </summary>
static String _temppath = "temp.bat";
public static String temppath
{
get { return ADSLIP._temppath; }
set { ADSLIP._temppath = value; }
}
/// <summary>
/// 字串拼接用
/// </summary>
private static StringBuilder sb = new StringBuilder();
/// <summary>
/// 撥號等待 預設15秒
/// </summary>
public static int delay = 15;
#endregion
#region 方法
/// <summary>
/// 開始撥號
/// </summary>
/// <param name="ADSL_Name">寬頻連線名稱</param>
/// <param name="ADSL_UserName">寬頻連線使用者名稱</param>
/// <param name="ADSL_PassWord">寬頻連線密碼</param>
public static void ChangeIp(String ADSL_Name = "寬頻連線", String ADSL_UserName = "", String ADSL_PassWord = "")
{
sb.Clear();
sb.AppendLine("@echo off");
sb.AppendLine("set adslmingzi=" + ADSL_Name);
sb.AppendLine("set adslzhanghao=" + ADSL_UserName);
sb.AppendLine("set adslmima=" + ADSL_PassWord);
sb.AppendLine("@Rasdial %adslmingzi% /disconnect");
sb.AppendLine("ping 127.0.0.1 -n 2");
sb.AppendLine("Rasdial %adslmingzi% %adslzhanghao% %adslmima%");
sb.AppendLine("echo 連線中");
sb.AppendLine("ping 127.0.0.1 -n 2");
sb.AppendLine("ipconfig");
// sb.AppendLine("pause");
using (StreamWriter sw = new StreamWriter(temppath, false, Encoding.Default))
{
sw.Write(sb.ToString());
}
Process.Start(temppath);
System.Threading.Thread.Sleep(delay * 1000);
while (!HttpMethod.CheckIp(null))
{
Process.Start(temppath);
System.Threading.Thread.Sleep(2 * delay * 1000);
}
File.Delete(temppath);
}
/// <summary>
/// 獲得本地ip
/// </summary>
/// <returns></returns>
public static String GetIP()
{
///獲取本地的IP地址
string AddressIP = string.Empty;
//遍歷
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
{
AddressIP = _IPAddress.ToString();
}
}
return AddressIP;
}
#endregion
}
}
幫助類說明
呼叫方式如下
ADSLIP.ChangeIp("寬頻名稱","寬頻使用者名稱","寬頻密碼");
此方式會出現控制檯介面(由於執行bat),但是會自行關閉
使用DotRas進行寬頻撥號
網上找的幫助類—-
/// <summary>
/// ADSL撥號幫助類 用DotRas實現
/// </summary>
public class RASDisplay
{
public static string ADSLName { get; set; }
public static string ADSLUserName { get; set; }
public static string ADSLPassWord { get; set; }
/// <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();
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,int delay = 10000)
{
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;
}
finally
{
Thread.Sleep(delay);
}
}
/// <summary>
/// 連線
/// </summary>
/// <returns></returns>
public static bool Connect()
{
try
{
CreateOrUpdatePPPOE(ADSLName);
using (RasDialer dialer = new RasDialer())
{
dialer.EntryName = ADSLName;
dialer.AllowUseStoredCredentials = true;
dialer.Timeout = 1000;
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
dialer.Credentials = new NetworkCredential(ADSLUserName, ADSLPassWord);
dialer.Dial();
return true;
}
}
catch (RasException re)
{
Console.WriteLine(re.Message);
return false;
}
}
}