This article is a mirror article of machine translation, please click here to jump to the original article.

View: 22553|Reply: 0

[Source] How to send a VPN

[Copy link]
Posted on 8/31/2015 9:23:25 PM | | |
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Diagnostics;
  7. using DotRas;

  8. namespace RASCONN
  9. {
  10.    public  class VPNHelper
  11.     {
  12.         // 系统路径 C:\windows\system32\
  13.         private static string WinDir = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"";

  14.         // rasdial.exe
  15.         private static string RasDialFileName = "rasdial.exe";

  16.         // VPN路径 C:\windows\system32\rasdial.exe
  17.         private static string VPNPROCESS = WinDir + RasDialFileName;

  18.         // VPN地址
  19.         public string IPToPing { get; set; }

  20.         // VPN名称
  21.         public string VPNName { get; set; }

  22.         // VPN用户名
  23.         public string UserName { get; set; }

  24.         // VPN密码
  25.         public string PassWord { get; set; }


  26.         public VPNHelper()
  27.         {

  28.         }

  29.         /// <summary>
  30.         /// 带参构造函数
  31.         /// </summary>
  32.         /// <param name="_vpnIP"></param>
  33.         /// <param name="_vpnName"></param>
  34.         /// <param name="_userName"></param>
  35.         /// <param name="_passWord"></param>
  36.         public VPNHelper(string _vpnIP, string _vpnName, string _userName, string _passWord)
  37.         {
  38.             this.IPToPing = _vpnIP;
  39.             this.VPNName = _vpnName;
  40.             this.UserName = _userName;
  41.             this.PassWord = _passWord;
  42.         }

  43.         /// <summary>
  44.         /// 尝试连接VPN(默认VPN)
  45.         /// </summary>
  46.         /// <returns></returns>
  47.         public void TryConnectVPN()
  48.         {
  49.             this.TryConnectVPN(this.VPNName, this.UserName, this.PassWord);
  50.         }

  51.         /// <summary>
  52.         /// 尝试断开连接(默认VPN)
  53.         /// </summary>
  54.         /// <returns></returns>
  55.         public void TryDisConnectVPN()
  56.         {
  57.             this.TryDisConnectVPN(this.VPNName);
  58.         }

  59.         /// <summary>
  60.         /// 创建或更新一个默认的VPN连接
  61.         /// </summary>
  62.         public void CreateOrUpdateVPN()
  63.         {
  64.             this.CreateOrUpdateVPN(this.VPNName, this.IPToPing);
  65.         }

  66.         /// <summary>
  67.         /// 尝试删除连接(默认VPN)
  68.         /// </summary>
  69.         /// <returns></returns>
  70.         public void TryDeleteVPN()
  71.         {
  72.             this.TryDeleteVPN(this.VPNName);
  73.         }
  74.         /// <summary>
  75.         /// 尝试连接VPN(指定VPN名称,用户名,密码)
  76.         /// </summary>
  77.         /// <returns></returns>
  78.         public void TryConnectVPN(string connVpnName, string connUserName, string connPassWord)
  79.         {
  80.             try
  81.             {
  82.                 string args = string.Format("{0} {1} {2}", connVpnName, connUserName, connPassWord);

  83.                 ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);

  84.                 myProcess.CreateNoWindow = true;

  85.                 myProcess.UseShellExecute = false;

  86.                 Process.Start(myProcess);

  87.             }
  88.             catch (Exception Ex)
  89.             {
  90.                 Debug.Assert(false, Ex.ToString());
  91.             }
  92.         }

  93.         /// <summary>
  94.         /// 尝试断开VPN(指定VPN名称)
  95.         /// </summary>
  96.         /// <returns></returns>
  97.         public void TryDisConnectVPN(string disConnVpnName)
  98.         {
  99.             try
  100.             {
  101.                 string args = string.Format(@"{0} /d", disConnVpnName);

  102.                 ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);

  103.                 myProcess.CreateNoWindow = true;

  104.                 myProcess.UseShellExecute = false;

  105.                 Process.Start(myProcess);

  106.             }
  107.             catch (Exception Ex)
  108.             {
  109.                 Debug.Assert(false, Ex.ToString());
  110.             }
  111.         }

  112.         /// <summary>
  113.         /// 创建或更新一个VPN连接(指定VPN名称,及IP)
  114.         /// </summary>
  115.         public void CreateOrUpdateVPN(string updateVPNname, string updateVPNip)
  116.         {
  117.             RasDialer dialer = new RasDialer();
  118.             RasPhoneBook allUsersPhoneBook = new RasPhoneBook();

  119.             allUsersPhoneBook.Open();

  120.             // 如果已经该名称的VPN已经存在,则更新这个VPN服务器地址
  121.             if (allUsersPhoneBook.Entries.Contains(updateVPNname))
  122.             {
  123.                 allUsersPhoneBook.Entries[updateVPNname].PhoneNumber = updateVPNip;
  124.                 // 不管当前VPN是否连接,服务器地址的更新总能成功,如果正在连接,则需要VPN重启后才能起作用
  125.                 allUsersPhoneBook.Entries[updateVPNname].Update();
  126.             }
  127.             // 创建一个新VPN
  128.             else
  129.             {
  130.                 RasEntry entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip, RasVpnStrategy.PptpFirst, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));

  131.                 allUsersPhoneBook.Entries.Add(entry);

  132.                 dialer.EntryName = updateVPNname;

  133.                 dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
  134.                
  135.             }
  136.         }

  137.         /// <summary>
  138.         /// 删除指定名称的VPN
  139.         /// 如果VPN正在运行,一样会在电话本里删除,但是不会断开连接,所以,最好是先断开连接,再进行删除操作
  140.         /// </summary>
  141.         /// <param name="delVpnName"></param>
  142.         public void TryDeleteVPN(string delVpnName)
  143.         {
  144.             RasDialer dialer = new RasDialer();
  145.             RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
  146.             allUsersPhoneBook.Open();
  147.             if (allUsersPhoneBook.Entries.Contains(delVpnName))
  148.             {
  149.                 allUsersPhoneBook.Entries.Remove(delVpnName);
  150.             }
  151.         }

  152.         /// <summary>
  153.         /// 获取当前正在连接中的VPN名称
  154.         /// </summary>
  155.         public List<string> GetCurrentConnectingVPNNames()
  156.         {
  157.             List<string> ConnectingVPNList = new List<string>();

  158.             Process proIP = new Process();

  159.             proIP.StartInfo.FileName = "cmd.exe ";
  160.             proIP.StartInfo.UseShellExecute = false;
  161.             proIP.StartInfo.RedirectStandardInput = true;
  162.             proIP.StartInfo.RedirectStandardOutput = true;
  163.             proIP.StartInfo.RedirectStandardError = true;
  164.             proIP.StartInfo.CreateNoWindow = true;//不显示cmd窗口
  165.             proIP.Start();

  166.             proIP.StandardInput.WriteLine(RasDialFileName);
  167.             proIP.StandardInput.WriteLine("exit");

  168.             // 命令行运行结果
  169.             string strResult = proIP.StandardOutput.ReadToEnd();
  170.             proIP.Close();

  171.             Regex regger = new Regex("(?<=已连接\r\n n)*(?=命令已完成)", RegexOptions.Multiline);

  172.             // 如果匹配,则说有正在连接的VPN
  173.             if (regger.IsMatch(strResult))
  174.             {
  175.                 string[] list = regger.Match(strResult).Value.ToString().Split('\n');
  176.                 for (int index = 0; index < list.Length; index++)
  177.                 {
  178.                     if (list[index] != string.Empty)
  179.                         ConnectingVPNList.Add(list[index].Replace("\r", ""));
  180.                 }
  181.             }
  182.             // 没有正在连接的VPN,则直接返回一个空List<string>
  183.             return ConnectingVPNList;
  184.         }

  185.     }
  186. }
Copy code






Previous:asp.net The code user under the mvc project did not handle the TypeInitializationException
Next:The 1:1 ratio is completely imitated by the source code transfer of the WeChat APP project
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com