1.使用VS2010建立Winform程序,界面如下:
2.编写代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetFwTypeLib;
namespace ChangeFireWallSetting
{
public partial class Form1 : Form
{
//创建firewall管理类的实例
INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
INetFwPolicy2 mgrWithFwPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false));
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
UpdateStateToUI();
}
private void UpdateStateToUI()
{
try
{
bool isEnable1 = mgrWithFwPolicy.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE);//家庭网络
bool isEnable2 = mgrWithFwPolicy.get_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC);//公共网络
if (isEnable1 != isEnable2)
{
mgrWithFwPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, isEnable1);
}
rad_close.Checked = !isEnable1;
if (isEnable1)
{
//允许例外
isEnable1 = mgrWithFwPolicy.get_BlockAllInboundTraffic(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE) ;// (NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE);
isEnable2 = mgrWithFwPolicy.get_BlockAllInboundTraffic(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC);
if (isEnable1 != isEnable2)
{
mgrWithFwPolicy.set_BlockAllInboundTraffic(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, isEnable1);
}
chk_allowPass.Checked =!isEnable1;
//阻止程序时是否通知我
isEnable1 = mgrWithFwPolicy.get_NotificationsDisabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE);
isEnable2 = mgrWithFwPolicy.get_NotificationsDisabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC);
if (isEnable1 != isEnable2)
{
mgrWithFwPolicy.set_NotificationsDisabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, isEnable1);
}
chk_AllowRemind.Checked = !isEnable1;
}
}
catch (Exception ex)
{
}
}
private void rad_close_CheckedChanged(object sender, EventArgs e)
{
chk_allowPass.Enabled = rad_open.Checked;
chk_AllowRemind.Enabled = rad_open.Checked;
}
private void btn_confirm_Click(object sender, EventArgs e)
{
if (rad_close.Checked)
{
System.ServiceProcess.ServiceController sec = new System.ServiceProcess.ServiceController("Security Center");
if (sec.CanStop)
sec.Stop();
}
else
{
mgrWithFwPolicy.set_BlockAllInboundTraffic(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, !chk_allowPass.Checked);
mgrWithFwPolicy.set_BlockAllInboundTraffic(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, !chk_allowPass.Checked);
mgrWithFwPolicy.set_NotificationsDisabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, !chk_AllowRemind.Checked);
mgrWithFwPolicy.set_NotificationsDisabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, !chk_AllowRemind.Checked);
}
mgrWithFwPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE , !rad_close.Checked);
mgrWithFwPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC , !rad_close.Checked);
}
private void btn_resetSetting_Click(object sender, EventArgs e)
{
//netFwMgr.RestoreDefaults();//也可以成功恢复默认设置
mgrWithFwPolicy.RestoreLocalFirewallDefaults();
UpdateStateToUI();
}
private void btn_addPermit_Click(object sender, EventArgs e)
{
//创建一个认证程序类的实例
INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));
//在例外列表里,程序显示的名称
app.Name = Application.ProductName;
//程序的决定路径,这里使用程序本身
app.ProcessImageFileName = Application.ExecutablePath;
app.Enabled = true;//是否启用该规则
//加入到防火墙的管理策略
try
{
string str = netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Item(app.ProcessImageFileName).Name;//不抛出异常表示已经存在本应用的例外
}
catch (Exception ex)
{
netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
}
}
private void btn_removePermit_Click(object sender, EventArgs e)
{
//参数为程序的绝对路径
netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(Application.ExecutablePath);
}
}
}
3.程序写好后,还有很重要的权限问题,有管理员权限才能对系统设置进行修改,所以我们需要使程序使用管理员权限运行。
VISTA和 Windows 7 都使用了UAC来控制程序访问,对于一些需要使用管理员身份运行的程序就得右键以管理员身份运行。C# 编程中可以使程序自动使用管理员身份运行,也就是我们常常看到一些程序的图标上会冒出个盾牌。具体操作如下:
3.1 在Visual Studio的“解决方案资源管理器”里双击打开“Properties”打开项目属性,找到“安全性”选项;
3.2 勾选“启用ClickOnce安全设置(N)”;
3.2 这时属性列表下会有出现“app.manifest”,双击打开;
3.3找到<requestedExecutionLevel level="asInvoker"uiAccess="false" />,并改为<requestedExecutionLevel level="requireAdministrator"uiAccess="false" />;
改为
3.4再到属性界面去除之前的勾选“启用ClickOnce安全设置(N)”,重新编译;
F5运行时,出现下面弹窗,点击“使用其他凭据重新启动”即可,VS将自动重新启动;
重新生成的应用程序的图标会出现一个小盾牌。
本文介绍如何使用VS2010和Winform开发一个简易防火墙配置工具,通过C#代码控制Windows防火墙的开关状态、例外程序及通知设置,并确保程序具备管理员权限。
2757

被折叠的 条评论
为什么被折叠?



