C# 连接自动拨号与断开分析

源网址:http://blog.csdn.net/hinyunsin/article/details/4033753

C#的自动拨号和断开当前连接的代码网上很多,可是没有一个是真正自动的,就算我指明了对那个连接进行拨号,也只是弹出那个拨号连接的窗口,想要拨号还需要自己手动点击“拨号”按钮,才能开始拨号。 到网上找了很久,终于找到了解决方法。

 

现在把源码贴出来,给大家研究分析用。

 

拨号控制管理类:

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Timers;  
  5. using System.Runtime.InteropServices;  
  6. using Microsoft.Win32;  
  7.   
  8. namespace ConnectionDial  
  9. {  
  10.     public class Ras  
  11.     {  
  12.         // Fields  
  13.         private bool bConnected;  
  14.         private ConnectionNotify ConnectNotify;  
  15.         private const int DNLEN = 15;  
  16.         private string EntryName;  
  17.         private const int ERROR_BUFFER_TOO_SMALL = 0x25b;  
  18.         private int hrasconn;  
  19.         public const int MAX_PATH = 260;  
  20.         public Timer NotifyTimer;  
  21.         private const int PWLEN = 0x100;  
  22.         private const string Ras_Authenticate = "正在验证用户名与密码.";  
  23.         public const string Ras_Connected = "成功连接到";  
  24.         public const string Ras_Connecting = "正在连接";  
  25.         private const string Ras_DialUping = "正在拨...";  
  26.         public const string Ras_Disconnected = "连接中断.";  
  27.         private const string Ras_Dot = "...";  
  28.         private const int RAS_MaxCallbackNumber = 0x80;  
  29.         private const int RAS_MaxDeviceName = 0x80;  
  30.         private const int RAS_MaxDeviceType = 0x10;  
  31.         public const int RAS_MaxEntryName = 0x100;  
  32.         private const int RAS_MaxPhoneNumber = 0x80;  
  33.         private const string Ras_OpenPort = "正在打开端口...";  
  34.         private const string Ras_PortOpend = "端口已经打开.";  
  35.         private RASCONN[] Rasconn;  
  36.         private const int RASCS_DONE = 0x2000;  
  37.         private const int RASCS_PAUSED = 0x1000;  
  38.         private const int UNLEN = 0x100;  
  39.   
  40.         // Methods  
  41.         public Ras()  
  42.         {  
  43.         }  
  44.   
  45.         public Ras(ConnectionNotify ConnectionDelegate, double interval)  
  46.         {  
  47.             this.ConnectNotify = ConnectionDelegate;  
  48.             this.NotifyTimer = new Timer(interval);  
  49.             this.NotifyTimer.Elapsed += new ElapsedEventHandler(this.TimerEvent);  
  50.             this.Rasconn = new RASCONN[1];  
  51.             this.Rasconn[0].dwSize = Marshal.SizeOf(this.Rasconn[0]);  
  52.             this.NotifyTimer.Start();  
  53.             this.bConnected = false;  
  54.         }  
  55.   
  56.         public bool CreateEntry(int hWnd, out string strError)  
  57.         {  
  58.             int nErrorValue = RasCreatePhonebookEntry(hWnd, null);  
  59.             if (nErrorValue == 0)  
  60.             {  
  61.                 strError = null;  
  62.                 return true;  
  63.             }  
  64.             strError = this.GetErrorString(nErrorValue);  
  65.             return false;  
  66.         }  
  67.   
  68.         public bool DeleteEntry(string strEntryName, out string strError)  
  69.         {  
  70.             int nErrorValue = RasDeleteEntry(null, strEntryName);  
  71.             if (nErrorValue == 0)  
  72.             {  
  73.                 strError = null;  
  74.                 return true;  
  75.             }  
  76.             strError = this.GetErrorString(nErrorValue);  
  77.             return false;  
  78.         }  
  79.   
  80.         public bool DialUp(string strEntryName, out string strError)  
  81.         {  
  82.             bool lpfPassword = false;  
  83.             RASDIALPARAMS structure = new RASDIALPARAMS();  
  84.             structure.dwSize = Marshal.SizeOf(structure);  
  85.             structure.szEntryName = strEntryName;  
  86.             RasDialEvent lpvNotifier = new RasDialEvent(this.RasDialFunc);  
  87.             int nErrorValue = RasGetEntryDialParams(nullref structure, ref lpfPassword);  
  88.             if (nErrorValue != 0)  
  89.             {  
  90.                 strError = this.GetErrorString(nErrorValue);  
  91.                 return false;  
  92.             }  
  93.             this.ConnectNotify("正在连接" + structure.szEntryName + "...", 1);  
  94.             this.EntryName = strEntryName;  
  95.             this.hrasconn = 0;  
  96.             nErrorValue = RasDial(0, nullref structure, 0, lpvNotifier, ref this.hrasconn);  
  97.             if (nErrorValue != 0)  
  98.             {  
  99.                 strError = this.GetErrorString(nErrorValue);  
  100.                 this.ConnectNotify(strError, 3);  
  101.                 return false;  
  102.             }  
  103.             this.ConnectNotify("正在打开端口...", 1);  
  104.             strError = null;  
  105.             return true;  
  106.         }  
  107.   
  108.         public bool EditEntry(int hWnd, string strEntryName, out string strError)  
  109.         {  
  110.             int nErrorValue = RasEditPhonebookEntry(hWnd, null, strEntryName);  
  111.             if (nErrorValue == 0)  
  112.             {  
  113.                 strError = null;  
  114.                 return true;  
  115.             }  
  116.             strError = this.GetErrorString(nErrorValue);  
  117.             return false;  
  118.         }  
  119.   
  120.         public bool GetDefaultEntry(out string strEntry, out string strError)  
  121.         {  
  122.             RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/RAS AutoDial/Default");  
  123.             if (key != null)  
  124.             {  
  125.                 string str = (string)key.GetValue("DefaultInternet");  
  126.                 if ((str != null) && (str.Length > 0))  
  127.                 {  
  128.                     strEntry = str;  
  129.                     strError = null;  
  130.                     return true;  
  131.                 }  
  132.             }  
  133.             strEntry = null;  
  134.             strError = "注册表访问失败.";  
  135.             return false;  
  136.         }  
  137.   
  138.         public bool GetEntries(out string[] strEntryName, out string strError)  
  139.         {  
  140.             RASENTRYNAME[] lprasentryname = new RASENTRYNAME[1];  
  141.             lprasentryname[0].dwSize = Marshal.SizeOf(lprasentryname[0]);  
  142.             int lpcb = 0;  
  143.             int lpcEntries = 0;  
  144.             int nErrorValue = RasEnumEntries(nullnull, lprasentryname, ref lpcb, ref lpcEntries);  
  145.             switch (nErrorValue)  
  146.             {  
  147.                 case 0:  
  148.                     break;  
  149.   
  150.                 case 0x25b:  
  151.                     lprasentryname = new RASENTRYNAME[lpcEntries];  
  152.                     lprasentryname[0].dwSize = Marshal.SizeOf(lprasentryname[0]);  
  153.                     break;  
  154.   
  155.                 default:  
  156.                     strError = this.GetErrorString(nErrorValue);  
  157.                     strEntryName = null;  
  158.                     return false;  
  159.             }  
  160.             nErrorValue = RasEnumEntries(nullnull, lprasentryname, ref lpcb, ref lpcEntries);  
  161.             if (nErrorValue != 0)  
  162.             {  
  163.                 strError = this.GetErrorString(nErrorValue);  
  164.                 strEntryName = null;  
  165.                 return false;  
  166.             }  
  167.             strEntryName = new string[lpcEntries];  
  168.             for (int i = 0; i < lpcEntries; i++)  
  169.             {  
  170.                 strEntryName[i] = lprasentryname[i].szEntryName;  
  171.             }  
  172.             strError = null;  
  173.             return true;  
  174.         }  
  175.   
  176.         public bool GetEntryParams(string strEntryName, out string strPhoneNumber, out string strUserName, out string strPassword, out bool bRememberPassword, out string strError)  
  177.         {  
  178.             bool lpfPassword = false;  
  179.             RASDIALPARAMS structure = new RASDIALPARAMS();  
  180.             structure.dwSize = Marshal.SizeOf(structure);  
  181.             structure.szEntryName = strEntryName;  
  182.             int nErrorValue = RasGetEntryDialParams(nullref structure, ref lpfPassword);  
  183.             if (nErrorValue != 0)  
  184.             {  
  185.                 strError = this.GetErrorString(nErrorValue);  
  186.                 strPhoneNumber = null;  
  187.                 strUserName = null;  
  188.                 strPassword = null;  
  189.                 bRememberPassword = false;  
  190.                 strError = null;  
  191.                 return false;  
  192.             }  
  193.             strPhoneNumber = structure.szPhoneNumber;  
  194.             strUserName = structure.szUserName;  
  195.             if (lpfPassword)  
  196.             {  
  197.                 strPassword = structure.szPassword;  
  198.             }  
  199.             else  
  200.             {  
  201.                 strPassword = null;  
  202.             }  
  203.             bRememberPassword = lpfPassword;  
  204.             strError = null;  
  205.             return true;  
  206.         }  
  207.   
  208.         internal string GetErrorString(int nErrorValue)  
  209.         {  
  210.             if ((nErrorValue >= 600) && (nErrorValue < 0x2f2))  
  211.             {  
  212.                 int cBufSize = 0x100;  
  213.                 string lpszErrorString = new string(new char[cBufSize]);  
  214.                 if (RasGetErrorString(nErrorValue, lpszErrorString, cBufSize) != 0)  
  215.                 {  
  216.                     lpszErrorString = null;  
  217.                 }  
  218.                 if ((lpszErrorString != null) && (lpszErrorString.Length > 0))  
  219.                 {  
  220.                     return lpszErrorString;  
  221.                 }  
  222.             }  
  223.             return null;  
  224.         }  
  225.   
  226.         public bool HangUp(out string strError)  
  227.         {  
  228.             this.bConnected = false;  
  229.             if (this.hrasconn != 0)  
  230.             {  
  231.                 int nErrorValue = RasHangUp(this.hrasconn);  
  232.                 if (nErrorValue != 0)  
  233.                 {  
  234.                     strError = this.GetErrorString(nErrorValue);  
  235.                     this.ConnectNotify(strError, 0);  
  236.                     return false;  
  237.                 }  
  238.             }  
  239.             foreach (RASCONN rasconn in this.Rasconn)  
  240.             {  
  241.                 if (rasconn.hrasconn != 0)  
  242.                 {  
  243.                     int num2 = RasHangUp(rasconn.hrasconn);  
  244.                     if (num2 != 0)  
  245.                     {  
  246.                         strError = this.GetErrorString(num2);  
  247.                         this.ConnectNotify(strError, 0);  
  248.                         return false;  
  249.                     }  
  250.                 }  
  251.             }  
  252.             strError = null;  
  253.             this.ConnectNotify("连接中断.", 0);  
  254.             return true;  
  255.         }  
  256.   
  257.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  258.         private static extern int RasCreatePhonebookEntry(int hwnd, string lpszPhonebook);  
  259.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  260.         private static extern int RasDeleteEntry(string lpszPhonebook, string lpszEntry);  
  261.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  262.         private static extern int RasDial(int lpRasDialExtensions, string lpszPhonebook, ref RASDIALPARAMS lpRasDialParams, int dwNotifierType, RasDialEvent lpvNotifier, ref int lphRasConn);  
  263.         private void RasDialFunc(uint unMsg, RASCONNSTATE rasconnstate, int dwError)  
  264.         {  
  265.             if (dwError != 0)  
  266.             {  
  267.                 this.ConnectNotify(this.GetErrorString(dwError), 3);  
  268.                 this.bConnected = false;  
  269.                 if (this.hrasconn != 0)  
  270.                 {  
  271.                     int nErrorValue = RasHangUp(this.hrasconn);  
  272.                     if (nErrorValue == 0)  
  273.                     {  
  274.                         this.ConnectNotify("连接中断.", 0);  
  275.                     }  
  276.                     else  
  277.                     {  
  278.                         this.ConnectNotify(this.GetErrorString(nErrorValue), 0);  
  279.                     }  
  280.                 }  
  281.             }  
  282.             else  
  283.             {  
  284.                 if (rasconnstate == RASCONNSTATE.RASCS_PortOpened)  
  285.                 {  
  286.                     this.ConnectNotify("端口已经打开.", 1);  
  287.                 }  
  288.                 if (rasconnstate == RASCONNSTATE.RASCS_ConnectDevice)  
  289.                 {  
  290.                     this.ConnectNotify("正在拨...", 1);  
  291.                 }  
  292.                 if (rasconnstate == RASCONNSTATE.RASCS_Authenticate)  
  293.                 {  
  294.                     this.ConnectNotify("正在验证用户名与密码.", 1);  
  295.                 }  
  296.                 if (rasconnstate == RASCONNSTATE.RASCS_Connected)  
  297.                 {  
  298.                     this.bConnected = true;  
  299.                     this.ConnectNotify("成功连接到" + this.EntryName + '.', 2);  
  300.                 }  
  301.             }  
  302.         }  
  303.   
  304.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  305.         private static extern int RasEditPhonebookEntry(int hwnd, string lpszPhonebook, string lpszEntryName);  
  306.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  307.         private static extern int RasEnumConnections([In, Out] RASCONN[] lprasconn, ref int lpcb, ref int lpcConnections);  
  308.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  309.         private static extern int RasEnumEntries(string reserved, string lpszPhonebook, [In, Out] RASENTRYNAME[] lprasentryname, ref int lpcb, ref int lpcEntries);  
  310.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  311.         private static extern int RasGetConnectStatus(int hrasconn, ref RASCONNSTATUS lprasconnstatus);  
  312.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  313.         private static extern int RasGetEntryDialParams(string lpszPhonebook, ref RASDIALPARAMS lprasdialparams, ref bool lpfPassword);  
  314.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  315.         private static extern int RasGetErrorString(int uErrorValue, string lpszErrorString, int cBufSize);  
  316.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  317.         private static extern int RasHangUp(int hrasconn);  
  318.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  319.         private static extern int RasRenameEntry(string lpszPhonebook, string lpszOldEntry, string lpszNewEntry);  
  320.         [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]  
  321.         private static extern int RasSetEntryDialParams(string lpszPhonebook, ref RASDIALPARAMS lprasdialparams, bool fRemovePassword);  
  322.         public bool RenameEntry(string strOldEntry, string strNewEntry, out string strError)  
  323.         {  
  324.             int nErrorValue = RasRenameEntry(null, strOldEntry, strNewEntry);  
  325.             if (nErrorValue == 0)  
  326.             {  
  327.                 strError = null;  
  328.                 return true;  
  329.             }  
  330.             strError = this.GetErrorString(nErrorValue);  
  331.             return false;  
  332.         }  
  333.   
  334.         public void SetDefaultEntry(string strEntry)  
  335.         {  
  336.             if ((strEntry != null) && (strEntry.Length > 0))  
  337.             {  
  338.                 RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/RAS AutoDial/Default"true);  
  339.                 if (key == null)  
  340.                 {  
  341.                     key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE/Microsoft/RAS AutoDial/Default");  
  342.                 }  
  343.                 key.SetValue("DefaultInternet", strEntry);  
  344.             }  
  345.         }  
  346.   
  347.         public bool SetEntryParams(string strEntryName, string strPhoneNumber, string strUserName, string strPassword, bool bRememberPassword, out string strError)  
  348.         {  
  349.             RASDIALPARAMS structure = new RASDIALPARAMS();  
  350.             structure.dwSize = Marshal.SizeOf(structure);  
  351.             structure.szEntryName = strEntryName;  
  352.             structure.szPhoneNumber = strPhoneNumber;  
  353.             structure.szUserName = strUserName;  
  354.             structure.szPassword = strPassword;  
  355.             int nErrorValue = RasSetEntryDialParams(nullref structure, !bRememberPassword);  
  356.             if (nErrorValue != 0)  
  357.             {  
  358.                 strError = this.GetErrorString(nErrorValue);  
  359.                 return false;  
  360.             }  
  361.             strError = null;  
  362.             return true;  
  363.         }  
  364.   
  365.         private void TimerEvent(object sender, ElapsedEventArgs e)  
  366.         {  
  367.             RASCONNSTATUS structure = new RASCONNSTATUS();  
  368.             int lpcb = 0;  
  369.             int lpcConnections = 0;  
  370.             structure.dwSize = Marshal.SizeOf(structure);  
  371.             int nErrorValue = RasEnumConnections(this.Rasconn, ref lpcb, ref lpcConnections);  
  372.             switch (nErrorValue)  
  373.             {  
  374.                 case 0:  
  375.                     break;  
  376.   
  377.                 case 0x25b:  
  378.                     this.Rasconn = new RASCONN[lpcConnections];  
  379.                     lpcb = this.Rasconn[0].dwSize = Marshal.SizeOf(this.Rasconn[0]);  
  380.                     nErrorValue = RasEnumConnections(this.Rasconn, ref lpcb, ref lpcConnections);  
  381.                     break;  
  382.   
  383.                 default:  
  384.                     this.ConnectNotify(this.GetErrorString(nErrorValue), 3);  
  385.                     return;  
  386.             }  
  387.             if (nErrorValue != 0)  
  388.             {  
  389.                 this.ConnectNotify(this.GetErrorString(nErrorValue), 3);  
  390.             }  
  391.             else if ((lpcConnections < 1) && this.bConnected)  
  392.             {  
  393.                 this.bConnected = false;  
  394.                 this.ConnectNotify("连接中断.", 0);  
  395.             }  
  396.             else  
  397.             {  
  398.                 for (int i = 0; i < lpcConnections; i++)  
  399.                 {  
  400.                     nErrorValue = RasGetConnectStatus(this.Rasconn[i].hrasconn, ref structure);  
  401.                     if (nErrorValue != 0)  
  402.                     {  
  403.                         this.ConnectNotify(this.GetErrorString(nErrorValue), 3);  
  404.                         return;  
  405.                     }  
  406.                     if ((structure.rasconnstate == RASCONNSTATE.RASCS_Connected) && !this.bConnected)  
  407.                     {  
  408.                         this.bConnected = true;  
  409.                         this.ConnectNotify("成功连接到" + this.Rasconn[i].szEntryName + '.', 2);  
  410.                     }  
  411.                     if ((structure.rasconnstate == RASCONNSTATE.RASCS_Disconnected) && this.bConnected)  
  412.                     {  
  413.                         this.bConnected = false;  
  414.                         this.ConnectNotify("连接中断.", 0);  
  415.                     }  
  416.                 }  
  417.             }  
  418.         }  
  419.   
  420.         // Nested Types  
  421.         public delegate void ConnectionNotify(string strNotify, int bConnect);  
  422.   
  423.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  424.         private struct RASCONN  
  425.         {  
  426.             internal int dwSize;  
  427.             internal int hrasconn;  
  428.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]  
  429.             internal string szEntryName;  
  430.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x11)]  
  431.             internal string szDeviceType;  
  432.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x81)]  
  433.             internal string szDeviceName;  
  434.         }  
  435.   
  436.         private enum RASCONNSTATE  
  437.         {  
  438.             RASCS_AllDevicesConnected = 4,  
  439.             RASCS_AuthAck = 12,  
  440.             RASCS_AuthCallback = 8,  
  441.             RASCS_AuthChangePassword = 9,  
  442.             RASCS_Authenticate = 5,  
  443.             RASCS_Authenticated = 14,  
  444.             RASCS_AuthLinkSpeed = 11,  
  445.             RASCS_AuthNotify = 6,  
  446.             RASCS_AuthProject = 10,  
  447.             RASCS_AuthRetry = 7,  
  448.             RASCS_CallbackSetByCaller = 0x1002,  
  449.             RASCS_ConnectDevice = 2,  
  450.             RASCS_Connected = 0x2000,  
  451.             RASCS_DeviceConnected = 3,  
  452.             RASCS_Disconnected = 0x2001,  
  453.             RASCS_Interactive = 0x1000,  
  454.             RASCS_OpenPort = 0,  
  455.             RASCS_PasswordExpired = 0x1003,  
  456.             RASCS_PortOpened = 1,  
  457.             RASCS_PrepareForCallback = 15,  
  458.             RASCS_Projected = 0x12,  
  459.             RASCS_ReAuthenticate = 13,  
  460.             RASCS_RetryAuthentication = 0x1001,  
  461.             RASCS_SubEntryConnected = 0x13,  
  462.             RASCS_SubEntryDisconnected = 20,  
  463.             RASCS_WaitForCallback = 0x11,  
  464.             RASCS_WaitForModemReset = 0x10  
  465.         }  
  466.   
  467.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  468.         private struct RASCONNSTATUS  
  469.         {  
  470.             internal int dwSize;  
  471.             internal Ras.RASCONNSTATE rasconnstate;  
  472.             internal int dwError;  
  473.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x11)]  
  474.             internal string szDeviceType;  
  475.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x81)]  
  476.             internal string szDeviceName;  
  477.         }  
  478.   
  479.         private delegate void RasDialEvent(uint unMsg, Ras.RASCONNSTATE rasconnstate, int dwError);  
  480.   
  481.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  482.         private struct RASDIALPARAMS  
  483.         {  
  484.             internal int dwSize;  
  485.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]  
  486.             internal string szEntryName;  
  487.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x81)]  
  488.             internal string szPhoneNumber;  
  489.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x81)]  
  490.             internal string szCallbackNumber;  
  491.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]  
  492.             internal string szUserName;  
  493.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]  
  494.             internal string szPassword;  
  495.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x10)]  
  496.             internal string szDomain;  
  497.         }  
  498.   
  499.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  500.         private struct RASENTRYNAME  
  501.         {  
  502.             internal int dwSize;  
  503.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x101)]  
  504.             internal string szEntryName;  
  505.         }  
  506.     }  
  507. }  

 

测试代码[部分](界面部分请自己用VS拖出来):

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using ConnectionDial;  
  9.   
  10. namespace DialUpTest  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         private Ras ras;  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.             this.ras = new Ras(new Ras.ConnectionNotify(this.RasConnectNotify), 1000.0);  
  19.         }  
  20.   
  21.         private void Form1_Load(object sender, EventArgs e)  
  22.         {  
  23.             string[] strs;  
  24.             string str;  
  25.             this.ras.GetEntries(out strs, out str);  
  26.             foreach(string s in strs){  
  27.                 comboBox1.Items.Add(s);  
  28.             }  
  29.             comboBox1.SelectedIndex = 0;  
  30.             showAttr();  
  31.         }  
  32.         private void showAttr()  
  33.         {  
  34.             int index = comboBox1.SelectedIndex;  
  35.             if (index >= 0)  
  36.             {  
  37.                 string str;  
  38.                 string str2;  
  39.                 string str3;  
  40.                 string str4;  
  41.                 bool flag;  
  42.                 this.ras.GetEntryParams((string)comboBox1.SelectedItem, out str, out str2, out str3, out flag, out str4);  
  43.   
  44.                 this.textBox1.Text = str2;  
  45.                 this.textBox2.Text = str3;  
  46.             }  
  47.         }  
  48.   
  49.         private void btnConnect_Click(object sender, EventArgs e)  
  50.         {  
  51.             string str;  
  52.             ras.DialUp((string)comboBox1.SelectedItem,out str);  
  53.         }  
  54.   
  55.         private void btnDisconnect_Click(object sender, EventArgs e)  
  56.         {  
  57.             string str;  
  58.             ras.HangUp(out str);  
  59.         }  
  60.   
  61.         private void btnExit_Click(object sender, EventArgs e)  
  62.         {  
  63.             this.Close();  
  64.         }  
  65.   
  66.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
  67.         {  
  68.             showAttr();  
  69.         }  
  70.         public void RasConnectNotify(string strNotify, int Connect)  
  71.         {  
  72.             msgListBox.Items.Add(strNotify);  
  73.         }  
  74.     }  
  75. }  

 

可以看到,上面那个拨号管理类的功能非常强大,大家可以根据这个类进行与拨号有关的其他设置,比如属性修改,新建连接等。

 

附注:

    虽然文章贴了快一年了,拨号管理只是本人本科毕业设计中的一个模块。现在已经很少接触.net了,因为我现在的专业是嵌入式,不过如果需要,我乐意跟大家共同讨论。

    现在看到大家对这篇文章这么感兴趣,可能在做C#的网络应用开发的过程中,需要使用到诸如拨号连接管理等功能,我这里推荐一个非常优秀的第三方库 DotRas,这项工程是个.net版的windows RAS(remote access service)管理库,带源码、dll库及详细文档说明。

    大家可以上这里访问和下载:http://dotras.codeplex.com/ 

    祝大家学习开心,工作顺利~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值