登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

For lover

我的幸福生活

 
 
 

日志

 
 
 
 

C# VPN  

2009-10-17 08:45:39|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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()
        {
        }
    }
}

  评论这张
 
阅读(9884)| 评论(30)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018