C#.NET获取拨号连接的宽带连接方法

这篇文章主要介绍了C#.NET获取拨号连接的宽带连接方法,实例演示了一个C#封装的ADSL拨号连接类及其使用方法,需要的朋友可以参考下

本文实例讲述了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; } } ///  /// 获取本机的拨号名称,也就是本机上所有的拨号 ///  ///  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; ///  /// 实例化一个ASDL连接 ///  /// ASDL名称,如“宽带连接” /// 用户名 /// 密码 /// 窗口显示方式,默认为因此拨号过程 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; } ///  /// 拨号名称 ///  public String ASDLName { get { return this._asdlname; } set { this._asdlname = value; } } ///  /// 拨号进程的窗口方式 ///  public ProcessWindowStyle WindowStyle { get { return this._windowstyle; } set { this._windowstyle = value; } } private String _username = null;  //用户名 private String _password = null;  //密码 ///  /// 用户名 ///  public String Username { get { return this._username; } set { this._username = value; } } ///  /// 密码 ///  public String Password { get { return this._password; } set { this._password = value; } } ///  /// 开始拨号 ///  /// 返回拨号进程的返回值 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; } ///  /// 端口连接 ///  ///  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#程序设计有所帮助。

以上就是C#.NET获取拨号连接的宽带连接方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程