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