1. 程式人生 > >C#.NET 獲取撥號連線 寬頻連線

C#.NET 獲取撥號連線 寬頻連線

直接可以用,我在XP VS2010 NET3.5上測試通過。

首先是ASDL的封裝

class SinASDL
{
	//ASDL在登錄檔中的存放位置,這個是針對WinXP的,不知道Win7是否是這個,待驗證
	private static String _adlskeys = @"RemoteAccess\Profile";
	public static String adlskeys
	{
		get
		{
			return _adlskeys;
		}
	}

	/// <summary>
	/// 獲取本機的撥號名稱,也就是本機上所有的撥號
	/// </summary>
	/// <returns></returns>
	public static String[] GetASDLNames()
	{
		RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(adlskeys);
		if (RegKey != null)
			return RegKey.GetSubKeyNames();
		else
			return null;
	}


	private String _asdlname = null;
	private ProcessWindowStyle _windowstyle = ProcessWindowStyle.Hidden;


	/// <summary>
	/// 例項化一個ASDL連線
	/// </summary>
	/// <param name="asdlname">ASDL名稱,如“寬頻連線”</param>
	/// <param name="username">使用者名稱</param>
	/// <param name="password">密碼</param>
	/// <param name="windowstyle">視窗顯示方式,預設為因此撥號過程</param>
	public SinASDL(String asdlname, String username = null, String password = null, ProcessWindowStyle windowstyle = ProcessWindowStyle.Hidden)
	{
		this.ASDLName = asdlname;
		this.Username = username;
		this.Password = password;
		this.WindowStyle = windowstyle;
	}

	/// <summary>
	/// 撥號名稱
	/// </summary>
	public String ASDLName
	{
		get
		{
			return this._asdlname;
		}
		set
		{
			this._asdlname = value;
		}
	}

	/// <summary>
	/// 撥號程序的視窗方式
	/// </summary>
	public ProcessWindowStyle WindowStyle
	{
		get
		{
			return this._windowstyle;
		}
		set
		{
			this._windowstyle = value;
		}
	}

	private String _username = null;	//使用者名稱
	private String _password = null;	//密碼
	/// <summary>
	/// 使用者名稱
	/// </summary>
	public String Username
	{
		get
		{
			return this._username;
		}
		set
		{
			this._username = value;
		}
	}
	/// <summary>
	/// 密碼
	/// </summary>
	public String Password
	{
		get
		{
			return this._password;
		}
		set
		{
			this._password = value;
		}
	}



	/// <summary>
	/// 開始撥號
	/// </summary>
	/// <returns>返回撥號程序的返回值</returns>
	public int Connect()
	{
		Process pro = new Process();
		pro.StartInfo.FileName = "rasdial.exe";
		pro.StartInfo.Arguments = this.ASDLName + " " + this.Username + " " + this.Password;
		pro.StartInfo.WindowStyle = this.WindowStyle;
		pro.Start();
		pro.WaitForExit();
		return pro.ExitCode;
	}

	/// <summary>
	/// 埠連線
	/// </summary>
	/// <returns></returns>
	public int Disconnect()
	{
		Process pro = new Process();
		pro.StartInfo.FileName = "rasdial.exe";
		pro.StartInfo.Arguments = this.ASDLName + " /DISCONNECT";
		pro.StartInfo.WindowStyle = this.WindowStyle;
		pro.Start();
		pro.WaitForExit();
		return pro.ExitCode;
	}
}

下面是使用測試

			//SinASDL asdl = new SinASDL("寬頻連線", "08793312221", "123456");	//寬頻連線
			//使用列舉到的第一個進行撥號
			SinASDL asdl = new SinASDL(SinASDL.GetASDLNames()[0], "08793312221", "123456", System.Diagnostics.ProcessWindowStyle.Normal);
			if (asdl.Connect() == 0)
			{
				MessageBox.Show("Success");
			}
			else
			{
				MessageBox.Show("Fail");
			}

我自己測試的時候是通過的。

如果電腦上不止一個撥號的,那麼你可以用SinASDL.GetASDLNames()進行列舉。