基本信息
源码名称:C#创建宽带连接(PPPOE)并拨号换IP(win XP/win7下测试通过) DotRas实例源码下载
源码大小:0.19M
文件格式:.zip
开发语言:C#
更新时间:2016-03-16
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
winxp win7已测试通过可用



	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
			CreateConnect("PPPoEDial");
			btn_Hungup.Enabled = false;
		}
				
		public void CreateConnect(string ConnectName)
		{
			RasDialer dialer = new RasDialer();
			RasPhoneBook book = new RasPhoneBook();
			try {
				book.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User));
				if (book.Entries.Contains(ConnectName)) {
					book.Entries[ConnectName].PhoneNumber = " ";
					book.Entries[ConnectName].Update();
				} else {
					ReadOnlyCollection<RasDevice> readOnlyCollection = RasDevice.GetDevices();
					RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First();
					RasEntry entry = RasEntry.CreateBroadbandEntry(ConnectName, device);
					entry.PhoneNumber = " ";
					book.Entries.Add(entry);
				}
			} catch (Exception) {
				lb_Status.Text = "创建PPPoE连接失败";
			}
		}
		
		void Btn_DialupClick(object sender, EventArgs e)
		{
			try {
				string username = Username.Text.Replace("\\r","\r").Replace("\\n","\n");
				string password = Password.Text.ToString();
				RasDialer dialer = new RasDialer();
				dialer.EntryName = "PPPoEDial";
				dialer.PhoneNumber = " ";
				dialer.AllowUseStoredCredentials = true;
				dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
				dialer.Credentials = new NetworkCredential(username, password);
				dialer.Timeout = 1000;
				RasHandle myras = dialer.Dial();
				while (myras.IsInvalid) {
					lb_Status.Text = "拨号失败";
				}
				if (!myras.IsInvalid) {
					lb_Status.Text = "拨号成功! ";
					RasConnection conn = RasConnection.GetActiveConnectionByHandle(myras);
					RasIPInfo ipaddr = (RasIPInfo)conn.GetProjectionInfo(RasProjectionType.IP);
					lb_IPAddr.Text = "获得IP: "   ipaddr.IPAddress.ToString();
					btn_Dialup.Enabled = false;
					btn_Hungup.Enabled = true;
				}
			} catch (Exception) {
				lb_Status.Text = "拨号出现异常";
			}
		}
		
		void Btn_HungupClick(object sender, EventArgs e)
		{
			try {
				ReadOnlyCollection<RasConnection> conList = RasConnection.GetActiveConnections();
				foreach (RasConnection con in conList) {
					con.HangUp();
				}
				Thread.Sleep(1000);
				lb_Status.Text = "注销成功";
				lb_IPAddr.Text = "已注销";
				btn_Dialup.Enabled = true;
				btn_Hungup.Enabled = false;
			} catch (Exception) {
				lb_Status.Text = "注销出现异常";
			}
		}
	}