C# 写的 Windows 下快捷管理VPN工具
Changelogs
2021-06-11
更新分类、标签
2022-03-21
更新文章中中英文间隔
简介
使用 C# 写的 Windows 下快捷管理 VPN 工具,可以实现对本地 VPN 的增加,修改,删除管理
原理
- Windows 有自带的连接管理工具 rasphone ,可以实现对链接进行新建/修改/连接/删除/断开等操作
在cmd 输入 : rasphone /? 就可以查看所有命令操作信息
- 查看现在是否有 VPN 连接可以使用 rasdial 命令.
使用
- 由于对C#不是很熟悉,新增vpn时需要给名称前面添加 vpn 作为前缀 …
- 鼠标悬停在每一个功能按钮都有相应提示.操作非常简单.
- 鼠标移开窗体会自动隐藏,在屏幕的右上方显示当前状态线.(已连接:绿色 未连接:红色)
- 工具条固定在屏幕有上方.X轴大约在屏幕3/4的地方.
- 提供一个VPN列表和用户信息列表(VPN的帐号),VPN列表是通过获取系统的adsl连接
- 用户列表可以多个,具体在程序启动目录的app.config里面配置user节点.
处理VPN的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConciseVPN
{
/**
* VPN操作类
*/
class VPNTools
{
/**
* 连接指定名称的VPN
*/
public static bool connect(string vpnName, string username, string password)
{
String command = "rasdial " + vpnName + " " + username + " " + password;
String result = Command.execute(command);
if (result.IndexOf("已连接") > 0)
return true;
return false;
}
/**
* 断开指定VPN连接
*/
public static bool disConnect(String vpnName)
{
String command = "rasdial " + vpnName + " /disconnect";
String result = Command.execute(command);
if (result.IndexOf("没有连接") > 0)
return false;
return true;
}
/**
* 新建VPN连接
*/
public static void createConnection() {
String command = "rasphone -a" ;
Command.execute(command);
}
/**
* 删除VPN连接
*/
public static void deleteConnection(String vpnName)
{
String command = "rasphone -r " + vpnName;
Command.execute(command);
}
/**
* 修改VPN连接
*/
public static void editConnection(String vpnName)
{
String command = "rasphone -e " + vpnName;
Command.execute(command);
}
/**
* 检测是否有VPN连接
*/
public static String checkCoonnect()
{
String result = Command.execute("rasdial");
if (result.IndexOf("没有连接") != -1)
return "false";
else{//返回连接中的VPN名称
result = result.Replace("已连接", "").Replace("命令已完成。","");
result = result.Substring(result.IndexOf("vpn_"));
result = result.Substring(0, result.IndexOf("\n\n\r\n"));
return result;
}
}
}
}
截图
VPN 连接成功,在屏幕右上方显示的绿色状态条(不要说你看不到….)

鼠标放到状态条显示的软件主界面

VPN 和用户列表

VPN 未连接状态
