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. }  

 

下面是使用測試

 

[csharp]  view plain copy print ?
  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM