问下大家,谁知道c# 如何进行vpn连接,调用那些dll文件和哪些函数,有没有例子,我想自己做一个界面进行vpn拨号和断开。谢谢大家

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using DotRas;
    using System.Net;namespace UpLoadTest
    {    public class VPN
        {
            private static string WinDir = Environment.GetFolderPath(Environment.SpecialFolder.System);
            private static string fileName = @"\rasdial.exe";//@"\rasphone.exe ";
            private static string VPNPROCESS = WinDir + fileName;       //private string _VPNConnectionName = "";        private string _IPToPing = "";        private bool _isConnected = false;        public bool IsConnected
            {
                get { return _isConnected; }
            }        public string IPToPing
            {
                get { return System.Configuration.ConfigurationSettings.AppSettings["serverIp"].ToString(); }
            }        public string VPNConnectionName
            {
                get { return System.Configuration.ConfigurationSettings.AppSettings["vpnName"].ToString(); }     
            }        public string UserName
            {
                get { return System.Configuration.ConfigurationSettings.AppSettings["userName"].ToString(); }
            }        public string Password
            {
                get
                {
                    return System.Configuration.ConfigurationSettings.AppSettings["password"].ToString();
                }
            }        public static bool TestConnection()
            {
                VPN vpn = new VPN();//为了以后更多属性,其实现在完全可以不要
                bool RV = false;
                try
                {
                    System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();                if (ping.Send(vpn.IPToPing).Status == System.Net.NetworkInformation.IPStatus.Success)
                    {
                        RV = true;
                    }
                    else
                    {
                        RV = false;
                    }
                    ping = null;
                }
                catch (Exception Ex)
                {
                    Debug.Assert(false, Ex.ToString());
                    RV = false;
                }
                return RV;
            }        public static bool ConnectToVPN()
            {
                VPN vpn = new VPN();
                bool RV = false;
                try
                {
                                   string args = string.Format("{0} {1} {2}",vpn.VPNConnectionName,vpn.UserName,vpn.Password);
                    ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS,args);                //System.Security.SecureString secretString = new System.Security.SecureString();
                    //foreach (char c in vpn.Password)
                    //    secretString.AppendChar(c);
                    //myProcess.Arguments = vpn.VPNConnectionName;
                    //myProcess.UserName = vpn.UserName;                //myProcess.Password = secretString;                //myProcess.Domain = "@ADServer.Local";
                    myProcess.CreateNoWindow = true;                myProcess.UseShellExecute = false;            
                    
                    //Process.Start(VPNPROCESS, );
                  
                    //Process.Start(VPNPROCESS, " -d " + vpn.VPNConnectionName);
                    Process.Start(myProcess);
                    //System.Windows.Forms.Application.DoEvents();
                    //System.Threading.Thread.Sleep(2000);
                   // System.Windows.Forms.Application.DoEvents();
                    RV = true;            }
                catch (Exception Ex)
                {
                    Debug.Assert(false, Ex.ToString());
                    RV = false;
                }
                return RV;
            }        public static bool DisconnectFromVPN()
            {            VPN vpn = new VPN();
                bool RV = false;
                try
                {
                    //System.Diagnostics.Process.Start(VPNPROCESS, " -h " + vpn.VPNConnectionName);
                    //System.Diagnostics.Process.Start(VPNPROCESS, string.Format(@"{0} /d",vpn.VPNConnectionName));
                    string args = string.Format(@"{0} /d", vpn.VPNConnectionName);
                    ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);
                    myProcess.CreateNoWindow = true;
                    myProcess.UseShellExecute = false; 
                    System.Diagnostics.Process.Start(myProcess);                //System.Windows.Forms.Application.DoEvents();
                    //System.Threading.Thread.Sleep(2000);
                    //System.Windows.Forms.Application.DoEvents();
                    RV = true;
                }
                catch (Exception Ex)
                {
                    Debug.Assert(false, Ex.ToString());
                    RV = false;
                }
                return RV;
            }        public static void CreateVPN()
            {
                VPN vpn = new VPN();
                DotRas.RasDialer dialer = new DotRas.RasDialer();
                DotRas.RasPhoneBook allUsersPhoneBook = new DotRas.RasPhoneBook();
                allUsersPhoneBook.Open();
                if (allUsersPhoneBook.Entries.Contains(vpn.VPNConnectionName))
                {
                    return;
                }
                RasEntry entry = RasEntry.CreateVpnEntry(vpn.VPNConnectionName, vpn.IPToPing, RasVpnStrategy.PptpFirst, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));            allUsersPhoneBook.Entries.Add(entry);
                dialer.EntryName = vpn.VPNConnectionName;
                dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                try
                {
                    dialer.DialAsync(new NetworkCredential(vpn.UserName, vpn.Password));
                }
                catch (Exception)
                {
                    return; 
                }
            }
            public VPN()
            {
            }
        }
    }
     
      

  2.   

    下面是一篇参考文章:VPN连接,调用,断开
    http://blog.csdn.net/emailqjc/archive/2009/10/14/4670094.aspx
      

  3.   

    一楼和二楼的方法我试了,这样的方法只是会在本机的网络连接里面创建一个虚拟专用连接,我想的是不用系统带的这个界面连接,我有自己的界面,我只是调用系统的一些api进行连接,一楼二楼的方法好像系统创建连接了后再连接就连不上了,三楼的文章我看了,呵呵,好像不是c# 的没看太明白,但是还要谢谢你们。
      

  4.   

    有没遇到拨VPN 拨多了就会卡在正在连接状态,想删除和断开都不行?