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

  1. class SinASDL  
  2. {  
  3.     //ASDL在注册表中的存放位置,这个是针对WinXP的,不知道Win7是否是这个,待验证  
  4.     private static String _adlskeys = @"RemoteAccess\Profile";  
  5.     public static String adlskeys  
  6.     {  
  7.         get  
  8.         {  
  9.             return _adlskeys;  
  10.         }  
  11.     }  
  12.   
  13.     /// <summary>  
  14.     /// 获取本机的拨号名称,也就是本机上所有的拨号  
  15.     /// </summary>  
  16.     /// <returns></returns>  
  17.     public static String[] GetASDLNames()  
  18.     {  
  19.         RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(adlskeys);  
  20.         if (RegKey != null)  
  21.             return RegKey.GetSubKeyNames();  
  22.         else  
  23.             return null;  
  24.     }  
  25.   
  26.   
  27.     private String _asdlname = null;  
  28.     private ProcessWindowStyle _windowstyle = ProcessWindowStyle.Hidden;  
  29.   
  30.   
  31.     /// <summary>  
  32.     /// 实例化一个ASDL连接  
  33.     /// </summary>  
  34.     /// <param name="asdlname">ASDL名称,如“宽带连接”</param>  
  35.     /// <param name="username">用户名</param>  
  36.     /// <param name="password">密码</param>  
  37.     /// <param name="windowstyle">窗口显示方式,默认为因此拨号过程</param>  
  38.     public SinASDL(String asdlname, String username = null, String password = null, ProcessWindowStyle windowstyle = ProcessWindowStyle.Hidden)  
  39.     {  
  40.         this.ASDLName = asdlname;  
  41.         this.Username = username;  
  42.         this.Password = password;  
  43.         this.WindowStyle = windowstyle;  
  44.     }  
  45.   
  46.     /// <summary>  
  47.     /// 拨号名称  
  48.     /// </summary>  
  49.     public String ASDLName  
  50.     {  
  51.         get  
  52.         {  
  53.             return this._asdlname;  
  54.         }  
  55.         set  
  56.         {  
  57.             this._asdlname = value;  
  58.         }  
  59.     }  
  60.   
  61.     /// <summary>  
  62.     /// 拨号进程的窗口方式  
  63.     /// </summary>  
  64.     public ProcessWindowStyle WindowStyle  
  65.     {  
  66.         get  
  67.         {  
  68.             return this._windowstyle;  
  69.         }  
  70.         set  
  71.         {  
  72.             this._windowstyle = value;  
  73.         }  
  74.     }  
  75.   
  76.     private String _username = null;    //用户名  
  77.     private String _password = null;    //密码  
  78.     /// <summary>  
  79.     /// 用户名  
  80.     /// </summary>  
  81.     public String Username  
  82.     {  
  83.         get  
  84.         {  
  85.             return this._username;  
  86.         }  
  87.         set  
  88.         {  
  89.             this._username = value;  
  90.         }  
  91.     }  
  92.     /// <summary>  
  93.     /// 密码  
  94.     /// </summary>  
  95.     public String Password  
  96.     {  
  97.         get  
  98.         {  
  99.             return this._password;  
  100.         }  
  101.         set  
  102.         {  
  103.             this._password = value;  
  104.         }  
  105.     }  
  106.   
  107.   
  108.   
  109.     /// <summary>  
  110.     /// 开始拨号  
  111.     /// </summary>  
  112.     /// <returns>返回拨号进程的返回值</returns>  
  113.     public int Connect()  
  114.     {  
  115.         Process pro = new Process();  
  116.         pro.StartInfo.FileName = "rasdial.exe";  
  117.         pro.StartInfo.Arguments = this.ASDLName + " " + this.Username + " " + this.Password;  
  118.         pro.StartInfo.WindowStyle = this.WindowStyle;  
  119.         pro.Start();  
  120.         pro.WaitForExit();  
  121.         return pro.ExitCode;  
  122.     }  
  123.   
  124.     /// <summary>  
  125.     /// 端口连接  
  126.     /// </summary>  
  127.     /// <returns></returns>  
  128.     public int Disconnect()  
  129.     {  
  130.         Process pro = new Process();  
  131.         pro.StartInfo.FileName = "rasdial.exe";  
  132.         pro.StartInfo.Arguments = this.ASDLName + " /DISCONNECT";  
  133.         pro.StartInfo.WindowStyle = this.WindowStyle;  
  134.         pro.Start();  
  135.         pro.WaitForExit();  
  136.         return pro.ExitCode;  
  137.     }  
  138. }  

 

下面是使用测试

 

  1. //SinASDL asdl = new SinASDL("宽带连接", "08793312221", "123456");  //宽带连接  
  2. //使用枚举到的第一个进行拨号  
  3. SinASDL asdl = new SinASDL(SinASDL.GetASDLNames()[0], "08793312221""123456", System.Diagnostics.ProcessWindowStyle.Normal);  
  4. if (asdl.Connect() == 0)  
  5. {  
  6.     MessageBox.Show("Success");  
  7. }  
  8. else  
  9. {  
  10.     MessageBox.Show("Fail");  
  11. }  

 

出自:http://blog.csdn.net/trbbadboy/article/details/7688448

posted @ 2013-01-16 11:53  奇幻男孩  阅读(4189)  评论(0编辑  收藏  举报