Exemplo n.º 1
0
        public static bool TestConnection()
        {
            VPNHelper vpn = new VPNHelper();//为了以后更多属性,其实现在完全可以不要
            bool      RV  = false;

            try
            {
                System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();

                if (ping.Send(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);
        }
Exemplo n.º 2
0
        public static bool DeleteVPN()
        {
            VPNHelper vpn = new VPNHelper();
            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} -r {1}", VPNDelete, VPNConnectionName);
                ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);
                myProcess.CreateNoWindow  = false;
                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);
        }
Exemplo n.º 3
0
        public static void CreateVPN()
        {
            VPNHelper vpn = new VPNHelper();

            using (DotRas.RasDialer dialer = new DotRas.RasDialer())
            {
                DotRas.RasPhoneBook allUsersPhoneBook = new DotRas.RasPhoneBook();
                allUsersPhoneBook.Open();
                if (allUsersPhoneBook.Entries.Contains(VPNConnectionName))
                {
                    return;
                }
                RasEntry entry = RasEntry.CreateVpnEntry(VPNConnectionName, IPToPing, RasVpnStrategy.PptpFirst, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn));
                allUsersPhoneBook.Entries.Add(entry);
                dialer.EntryName     = VPNConnectionName;
                dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                try
                {
                    dialer.Credentials = new NetworkCredential(UserName, Password);
                    dialer.DialAsync();
                }
                catch (Exception)
                {
                    return;
                }
            }
        }
Exemplo n.º 4
0
        public static List <string> ReadConnection()
        {
            List <string> result = new List <string>();
            VPNHelper     vpn    = new VPNHelper();

            try
            {
                string  args      = VPNPROCESS;
                Process myProcess = new Process();
                myProcess.StartInfo.CreateNoWindow         = false;
                myProcess.StartInfo.UseShellExecute        = false;
                myProcess.StartInfo.RedirectStandardInput  = true;
                myProcess.StartInfo.RedirectStandardOutput = true;
                myProcess.StartInfo.FileName = "cmd.exe";
                myProcess.Start();
                myProcess.StandardInput.WriteLine(VPNPROCESS);
                myProcess.StandardInput.WriteLine("exit");
                myProcess.WaitForExit();

                string content   = myProcess.StandardOutput.ReadToEnd();
                Regex  regexlist = new Regex(@"已连接\s+?(?<list>[\s\S]+?)命令已完成。");
                if (regexlist.IsMatch(content))
                {
                    string list  = regexlist.Match(content).Groups["list"].Value;
                    var    array = list.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                    if (array.Length > 0)
                    {
                        result.AddRange(array);
                    }
                }

                myProcess.Close();
            }
            catch (Exception Ex)
            {
                Debug.Assert(false, Ex.ToString());
            }

            return(result);
        }
Exemplo n.º 5
0
        public static bool ConnectToVPN()
        {
            VPNHelper vpn = new VPNHelper();
            bool      RV  = false;

            try
            {
                string           args      = string.Format("{0} {1} {2}", VPNConnectionName, UserName, Password);
                ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);
                myProcess.CreateNoWindow = true;

                myProcess.UseShellExecute = false;
                Process.Start(myProcess);
                RV = true;
            }
            catch (Exception Ex)
            {
                Debug.Assert(false, Ex.ToString());
                RV = false;
            }
            return(RV);
        }