設置成這樣子
我是用網上找的dotras的代碼來建立vpn連接的。。英語水平不太好,不會把數據加密選線那裡改為可選加密
1.using DotRas;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace IP
{
class VPNHelper
{
// 系統路徑 C:\windows\system32\
private static string WinDir = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\";
// rasdial.exe
private static string RasDialFileName = "rasdial.exe";
// VPN路徑 C:\windows\system32\rasdial.exe
private static string VPNPROCESS = WinDir + RasDialFileName;
// VPN地址
public string IPToPing { get; set; }
// VPN名稱
public string VPNName { get; set; }
// VPN用戶名
public string UserName { get; set; }
// VPN密碼
public string PassWord { get; set; }
public VPNHelper()
{
}
/// <summary>
/// 帶參構造函數
/// </summary>
/// <param name="_vpnIP"></param>
/// <param name="_vpnName"></param>
/// <param name="_userName"></param>
/// <param name="_passWord"></param>
public VPNHelper(string _vpnIP, string _vpnName, string _userName, string _passWord)
{
this.IPToPing = _vpnIP;
this.VPNName = _vpnName;
this.UserName = _userName;
this.PassWord = _passWord;
}
/// <summary>
/// 嘗試連接VPN(默認VPN)
/// </summary>
/// <returns></returns>
public void TryConnectVPN()
{
this.TryConnectVPN(this.VPNName,this.UserName,this.PassWord);
}
/// <summary>
/// 嘗試斷開連接(默認VPN)
/// </summary>
/// <returns></returns>
public void TryDisConnectVPN()
{
this.TryDisConnectVPN(this.VPNName);
}
/// <summary>
/// 創建或更新一個默認的VPN連接
/// </summary>
public void CreateOrUpdateVPN()
{
this.CreateOrUpdateVPN(this.VPNName, this.IPToPing);
}
/// <summary>
/// 嘗試刪除連接(默認VPN)
/// </summary>
/// <returns></returns>
public void TryDeleteVPN()
{
this.TryDeleteVPN(this.VPNName);
}
/// <summary>
/// 嘗試連接VPN(指定VPN名稱,用戶名,密碼)
/// </summary>
/// <returns></returns>
public void TryConnectVPN(string connVpnName,string connUserName,string connPassWord)
{
try
{
string args = string.Format("{0} {1} {2}", connVpnName, connUserName, connPassWord);
ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);
myProcess.CreateNoWindow = true;
myProcess.UseShellExecute = false;
Process.Start(myProcess);
}
catch (Exception Ex)
{
Debug.Assert(false, Ex.ToString());
}
}
/// <summary>
/// 嘗試斷開VPN(指定VPN名稱)
/// </summary>
/// <returns></returns>
public void TryDisConnectVPN(string disConnVpnName)
{
try
{
string args = string.Format(@"{0} /d", disConnVpnName);
ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);
myProcess.CreateNoWindow = true;
myProcess.UseShellExecute = false;
Process.Start(myProcess);
}
catch (Exception Ex)
{
Debug.Assert(false, Ex.ToString());
}
}
/// <summary>
/// 創建或更新一個VPN連接(指定VPN名稱,及IP)
/// </summary>
public void CreateOrUpdateVPN(string updateVPNname,string updateVPNip)
{
RasDialer dialer = new RasDialer();
RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
allUsersPhoneBook.Open();
// 如果已經該名稱的VPN已經存在,則更新這個VPN服務器地址
if (allUsersPhoneBook.Entries.Contains(updateVPNname))
{
allUsersPhoneBook.Entries[updateVPNname].PhoneNumber = updateVPNip;
// 不管當前VPN是否連接,服務器地址的更新總能成功,如果正在連接,則需要VPN重啟後才能起作用
allUsersPhoneBook.Entries[updateVPNname].Update();
}
// 創建一個新VPN
else
{
RasEntry entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip, RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));
allUsersPhoneBook.Entries.Add(entry);
dialer.EntryName = updateVPNname;
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
}
}
/// <summary>
/// 刪除指定名稱的VPN
/// 如果VPN正在運行,一樣會在電話本裡刪除,但是不會斷開連接,所以,最好是先斷開連接,再進行刪除操作
/// </summary>
/// <param name="delVpnName"></param>
public void TryDeleteVPN(string delVpnName)
{
RasDialer dialer = new RasDialer();
RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
allUsersPhoneBook.Open();
if (allUsersPhoneBook.Entries.Contains(delVpnName))
{
allUsersPhoneBook.Entries.Remove(delVpnName);
}
}
/// <summary>
/// 獲取當前正在連接中的VPN名稱
/// </summary>
public List<string> GetCurrentConnectingVPNNames()
{
List<string> ConnectingVPNList = new List<string>();
Process proIP = new Process();
proIP.StartInfo.FileName = "cmd.exe ";
proIP.StartInfo.UseShellExecute = false;
proIP.StartInfo.RedirectStandardInput = true;
proIP.StartInfo.RedirectStandardOutput = true;
proIP.StartInfo.RedirectStandardError = true;
proIP.StartInfo.CreateNoWindow = true;//不顯示cmd窗口
proIP.Start();
proIP.StandardInput.WriteLine(RasDialFileName);
proIP.StandardInput.WriteLine("exit");
// 命令行運行結果
string strResult = proIP.StandardOutput.ReadToEnd();
proIP.Close();
Regex regger = new Regex("(?<=已連接\r\n)(.*\n)*(?=命令已完成)", RegexOptions.Multiline);
// 如果匹配,則說有正在連接的VPN
if (regger.IsMatch(strResult))
{
string[] list = regger.Match(strResult).Value.ToString().Split('\n');
for(int index = 0 ; index < list.Length;index++)
{
if (list[index]!=string.Empty)
ConnectingVPNList.Add(list[index].Replace("\r",""));
}
}
// 沒有正在連接的VPN,則直接返回一個空List<string>
return ConnectingVPNList;
}
}
}
VPN建立連接時候怎麼設置屬性:安全:數據加密:可選加密
##
RasEntry entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip,RasVpnStrategy.L2tpOnly,RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));
entry.EncryptionType = RasEncryptionType.Optional;//少了這個,這個就是設置屬性:安全:數據加密:可選加密
allUsersPhoneBook.Entries.Add(entry);
dialer.EntryName = updateVPNname;
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); ##