IP设置小工具

放假闲着摆弄Qt,写了个修改IP的小工具,功能十分简单,就是修改IP设置,保存设置,读取设置。
程序的基本原理就是对netsh的包装了,用netsh修改IP的基本命令如下:

netsh interface ip set address "本地连接" static 192.168.1.2 255.255.255.0  192.168.1.1 gwmetric=1
netsh interface ip set dns name="本地连接" source=static addr=8.8.4.4
netsh interface ip add dns "本地连接" 202.112.20.131

使用ShellExecute函数来执行CMD命令。

QString strSetIP = "netsh interface ip set address "本地连接" static 192.168.1.2 255.255.255.0  192.168.1.1 gwmetric=1";
ShellExecute(NULL, L"open", L"cmd.exe", reinterpret_cast<const wchar_t *>(strSetIP.utf16()), NULL, SW_HIDE);

另外对IP地址输入的合法性,可以使用正则表达式来检查。最基本的IP形式可以理解为使用“.”分隔的四个数字,检验字符串可写为:

QRegExp strIP = "d+.d+.d+.d+";

对于进一步的检查,我们可以使用以下条件:
1.取值为0~255之间。
2.IP地址、子网掩码、网关的取值可能不为0.
则有如下表达式:

//IP可为零
QString strIPNum = "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)";
//IP不可为零
QString strIPNum = "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])";
QRegExp strIP = "^" + strIPNum + "." + strIPNum + "." + strIPNum + "." + strIPNum + "$";

恩,其实使用bat脚本也是挺方便的~~。

PS:NetConfig.7z
NetConfigCode.7z

继续阅读IP设置小工具