日期:2014-05-18  浏览次数:21135 次

c#实现vpn控制1
这里面引用了DotRas.dll大家可以到官网http://dotras.codeplex.com/下载
1.新建一个VPN
2.修改指定名字VPN的主机IP地址
3.监视当前连接状态
4.删除指定名称的VPN
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using DotRas;
using System.Text.RegularExpressions;
namespace VpnTest
{
    public 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()
        {
        }
        public VPNHelper(string _vpnIP, string _vpnName, string _userName, string _passWord)
        {
            this.IPToPing = _vpnIP;
            this.VPNName = _vpnName;
            this.UserName = _userName;
            this.PassWord = _passWord;
        }
        //尝试连接VPN(默认VPN)
        public void TryConnectVPN()
        {
            this.TryConnectVPN(this.VPNName,this.UserName,this.PassWord);
        }
        //尝试断开连接(默认VPN)
        public void TryDisConnectVPN()
        {
            this.TryDisConnectVPN(this.VPNName);
        }
        //创建或更新一个默认的VPN连接
        public void CreateOrUpdateVPN()
        {
            this.CreateOrUpdateVPN(this.VPNName, this.IPToPing);
        }
        //尝试删除连接(默认VPN)
        public void TryDeleteVPN()
        {
            this.TryDeleteVPN(this.VPNName);
        }
        //尝试连接VPN(指定VPN名称,用户名,密码)
        public void TryConnectVPN(string connVpnName,string connUserName,