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()进行枚举。
希望本文所述对大家的C#程序设计有所帮助。
赞 (0)