2 s ylli1 s_ylli1 于 2013.11.25 21:49 提问

VPN宽带连接的时候怎么获取他的错误代码。。比如VPN连接错误代码691,678。。之类的

using System;

using System.Collections.Generic;

using System.Text;

using System.Diagnostics;

using DotRas;

using System.Text.RegularExpressions;

namespace VpnTest

{

public class VPNHelper

{

// 系统路径 C:\windows\system32\

private static string WinDir = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"";

// rasdial.exe

private static string RasDialFileName = "rasdial.exe";

// VPN路径 C:\windows\system32\rasdial.exe

private static string VPNPROCESS = WinDir + RasDialFileName;

// VPN地址

public string IPToPing { get; set; }

// VPN名称

public string VPNName { get; set; }

// VPN用户名

public string UserName { get; set; }

// VPN密码

public string PassWord { get; set; }

public VPNHelper()

{

}

public VPNHelper(string _vpnIP, string _vpnName, string _userName, string _passWord)

{

this.IPToPing = _vpnIP;

this.VPNName = _vpnName;

this.UserName = _userName;

this.PassWord = _passWord;

}

//尝试连接VPN(默认VPN)

public void TryConnectVPN()

{

this.TryConnectVPN(this.VPNName,this.UserName,this.PassWord);

}

//尝试断开连接(默认VPN)

public void TryDisConnectVPN()

{

this.TryDisConnectVPN(this.VPNName);

}

//创建或更新一个默认的VPN连接

public void CreateOrUpdateVPN()

{

this.CreateOrUpdateVPN(this.VPNName, this.IPToPing);

}

//尝试删除连接(默认VPN)

public void TryDeleteVPN()

{

this.TryDeleteVPN(this.VPNName);

}

//尝试连接VPN(指定VPN名称,用户名,密码)

public void TryConnectVPN(string connVpnName,string connUserName,string connPassWord)

{

try

{

string args = string.Format("{0} {1} {2}", connVpnName, connUserName, connUserName);

ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);

myProcess.CreateNoWindow = true;

myProcess.UseShellExecute = false;

Process.Start(myProcess);

}

catch (Exception Ex)

{

Debug.Assert(false, Ex.ToString());

}

}

//尝试断开VPN(指定VPN名称)

public void TryDisConnectVPN(string disConnVpnName)

{

try

{

string args = string.Format(@"{0} /d", disConnVpnName);

ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);

myProcess.CreateNoWindow = true;

myProcess.UseShellExecute = false;

Process.Start(myProcess);

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

}

3个回答

s_ylli1
s_ylli1   2013.12.02 15:12
已采纳

好像是因为那个DotRas.dll没有返回报错机制
我最后使用CMD调用rasdial.exe
rasdial connVpnName connUserName connPassWord

u012957725
u012957725   2013.11.26 09:19

路过帮顶,好像是isp

u011569419
u011569419   2013.12.02 10:17

691 和 678 都是拨号登录的错误代码
你最好检查下vpn的帐号和密码是否有误

Csdn user default icon
上传中...
上传图片
插入图片