| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 396 人关注过本帖
标题:写好的vpn,运行报错。求高手指点
只看楼主 加入收藏
rxzfn1987
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-8-21
收藏
 问题点数:0 回复次数:0 
写好的vpn,运行报错。求高手指点
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using DotRas;
using

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
            {
                ping = new ();

                if (ping.Send(vpn.IPToPing).Status == )
                {
                    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()
        {
        }
    }
}
搜索更多相关主题的帖子: private public 
2012-08-21 18:13
快速回复:写好的vpn,运行报错。求高手指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.063442 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved