ADSL拨号连接

dotras是一个提供远程访问服务的组件,使用它可以方便的
进行宽带拨号连接

由于不同的系统中dotras调用的底层api可能不同,所以使用时要根据不同的系统使用不同的dotras。
dotras目前有以下几个包,在nuget中可以找到。每个包都有对应的系统

  • WIN8 - Windows 8, Windows 8 Server
  • WIN7 - Windows 7, Windows 2008 Server R2 (aka Windows 7 Server)
  • WIN2K8 - Windows Vista, Windows 2008 Server
  • WINXP - Windows XP, Windows 2003 Server
  • WIN2K - Windows 2000, Windows 2000 Server (baseline)

详细说明可以参照dotras doucument

以下是使用dotras进行的简单实现ADSL拨号连接。

        /// <summary>
		/// 连接
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, EventArgs e)
		{
			try
			{
				RasDialer dialer = new RasDialer();
				dialer.EntryName = "宽带连接";  //宽带连接的名字
				dialer.AllowUseStoredCredentials = true;
				dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
				dialer.Credentials = new System.Net.NetworkCredential("宽带账户", "宽带密码");
				dialer.Timeout = 1000;
				MessageBox.Show(dialer.PhoneBookPath);
				dialer.Dial();
				Thread.Sleep(100);
				var conns = RasConnection.GetActiveConnections();//获取当前所有活动连接
				var conn = conns.First();
				RasIPInfo ipAddresses = (RasIPInfo)conn.GetProjectionInfo(RasProjectionType.IP);
				MessageBox.Show(ipAddresses.IPAddress.ToString());

			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		
		/// <summary>
		/// 断开连接
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button2_Click(object sender, EventArgs e)
		{
			try
			{
				var conns = RasConnection.GetActiveConnections();//获取当前所有活动连接
				var conn = conns.FirstOrDefault(o => o.EntryName == "宽带连接");
				if (conn != null)
				{
					conn.HangUp();
					MessageBox.Show("断开连接成功");
				}
			}
			catch (Exception ex)
			{

				MessageBox.Show(ex.Message);
			}
		}
		/// <summary>
		/// 获取IP
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button3_Click(object sender, EventArgs e)
		{
			var conns = RasConnection.GetActiveConnections();//获取当前所有活动连接          
			var conn = conns.FirstOrDefault(o=>o.EntryName== "宽带连接");
			if (conn != null)
			{
			  sIPInfo ipAddresses = (RasIPInfo)conn.GetProjectionInfo(RasProjectionType.IP);
			  MessageBox.Show(ipAddresses.IPAddress.ToString());
			}
		}

备注:

rasdial命令也可以进行拨号操作

posted @ 2015-11-17 21:33  senki  阅读(392)  评论(0编辑  收藏  举报