问下大家,谁知道c# 如何进行vpn连接,调用那些dll文件和哪些函数,有没有例子,我想自己做一个界面进行vpn拨号和断开。谢谢大家
解决方案 »
- 能不能把字符串转化为对象
- 怎么得到注册表中某一项下所有的项的名称的数组
- 请问怎么比较2个datetime类型是否相同的问题
- 关于VS2005部署水晶报表的问题
- XML递归检索节点问题
- 求关于PDF格式的源代码
- C#开发的ERP,开放部分源码,寻求C#爱好者或者公司的合作(买断、代理、交叉授权均可)
- 使用 Remoting 传递的自定义集合类出现错误,有什么方法可在Remoting 中传递 集合对象
- 请问怎样在 鼠标移动的时候触发 OnPaint(PaintEventArgs e)?
- reportviewer控件在IE10以上版本显示缩成一团
- C#窗体程序欢迎界面删除
- 求救 如何获取Memcached中的所有KEY
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()
{
}
}
}
http://blog.csdn.net/emailqjc/archive/2009/10/14/4670094.aspx