资源简介
C#更改Windows防火墙设置实例程序
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NetFwTypeLib;//在添加引用时,选择 COM 选项卡, 找到 NetFwTypeLib
//Download by http://www.codefans.net
namespace WindowsFireWall
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//创建firewall管理类的实例
INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID(“HNetCfg.FwMgr“));
private void radioButton1_CheckedChanged(object sender EventArgs e)
{
checkBox1.Enabled = checkBox2.Enabled = radioButton1.Checked;
}
//加载时生成选中状态
private void Form1_Load(object sender EventArgs e)
{
bool isEnabled = netFwMgr.LocalPolicy.CurrentProfile.FirewallEnabled;
if (isEnabled)
{
//radioButton1.Checked = isEnabled;
checkBox1.Checked = !netFwMgr.LocalPolicy.CurrentProfile.ExceptionsNotAllowed;
checkBox2.Checked = !netFwMgr.LocalPolicy.CurrentProfile.NotificationsDisabled;
}
else {
//radioButton2.Checked = true;
}
}
//应用选择的防火墙策略
private void button4_Click(object sender EventArgs e)
{
bool isEnalbed = radioButton1.Checked;
if (!isEnalbed)
{
//防止关闭防火墙时右下角的提示,关闭安全中心服务
System.ServiceProcess.ServiceController sec = new System.ServiceProcess.ServiceController(“Security Center“);
if (sec.CanStop)
sec.Stop();
}
netFwMgr.LocalPolicy.CurrentProfile.FirewallEnabled = isEnalbed;
if (isEnalbed)
{
netFwMgr.LocalPolicy.CurrentProfile.ExceptionsNotAllowed = !checkBox1.Checked;
netFwMgr.LocalPolicy.CurrentProfile.NotificationsDisabled = !checkBox2.Checked;
}
}
//还原默认设置
private void button1_Click(object sender EventArgs e)
{
////获取管理防火墙的COM组件的type
//Type fwMgrType = Type.GetTypeFromProgID(“HNetCfg.FwMgr“true);
////创建一个COM组件的实例
//object fwMgr = Activator.CreateInstance(fwMgrType);
////调用 RestoreDefaults() 恢复默认值
//fwMgrType.InvokeMember(“RestoreDefaults“System.Reflection.BindingFlags.InvokeMethod null fwMgr null);
netFwMgr.RestoreDefaults();
Form1_Load(null null);
}
//创建一个认证程序类的实例
INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)Activator.CreateInstance(Type.GetTypeFromProgID(“HNetCfg.FwAuthorizedApplication“));
private void button2_Click(object sender EventArgs e)
{
//在例外列表里,程序显示的名称
app.Name =
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
----------- --------- ---------- ----- ----
94729 28
评论
共有 条评论