• 大小: 25KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-08
  • 语言: C#
  • 标签: 防火墙  

资源简介

c#操作防火墙代码-C#开启或关闭防火墙设置代码

资源截图

代码片段和文件信息

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

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 = “window应用程序“;
            //程序的决定路径

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        863  2009-03-16 15:33  WindowsFireWall\obj\WindowsFireWall.csproj.FileListAbsolute.txt

     文件      24576  2009-03-16 15:33  WindowsFireWall\bin\Debug\WindowsFireWall.exe

     文件      24576  2009-03-16 15:33  WindowsFireWall\obj\Debug\WindowsFireWall.exe

     文件       5632  2005-11-11 22:25  WindowsFireWall\bin\Debug\WindowsFireWall.vshost.exe

     文件      12288  2009-03-16 15:33  WindowsFireWall\bin\Debug\Interop.NetFwTypeLib.dll

     文件      12288  2009-03-16 15:33  WindowsFireWall\obj\Debug\Interop.NetFwTypeLib.dll

     文件        842  2009-03-16 15:33  WindowsFireWall\obj\Debug\WindowsFireWall.csproj.GenerateResource.Cache

     文件        496  2009-03-16 15:33  WindowsFireWall\obj\Debug\WindowsFireWall.csproj.ResolveComReference.cache

     文件       1188  2009-03-16 14:14  WindowsFireWall\Properties\AssemblyInfo.cs

     文件       3695  2009-03-16 21:51  WindowsFireWall\Form1.cs

     文件       7009  2009-03-16 15:33  WindowsFireWall\Form1.Designer.cs

     文件        474  2009-03-16 14:14  WindowsFireWall\Program.cs

     文件       2886  2009-03-16 14:14  WindowsFireWall\Properties\Resources.Designer.cs

     文件       1100  2009-03-16 14:14  WindowsFireWall\Properties\Settings.Designer.cs

     文件       3615  2009-03-16 15:25  WindowsFireWall\WindowsFireWall.csproj

     文件      22016  2009-03-16 15:33  WindowsFireWall\bin\Debug\WindowsFireWall.pdb

     文件      22016  2009-03-16 15:33  WindowsFireWall\obj\Debug\WindowsFireWall.pdb

     文件        180  2009-03-16 15:33  WindowsFireWall\obj\Debug\WindowsFireWall.Form1.resources

     文件        180  2009-03-16 15:33  WindowsFireWall\obj\Debug\WindowsFireWall.Properties.Resources.resources

     文件       5814  2009-03-16 15:33  WindowsFireWall\Form1.resx

     文件       5612  2009-03-16 14:14  WindowsFireWall\Properties\Resources.resx

     文件        249  2009-03-16 14:14  WindowsFireWall\Properties\Settings.settings

     文件        918  2009-03-16 14:14  WindowsFireWall\WindowsFireWall.sln

    ..A..H.     13312  2009-03-16 15:34  WindowsFireWall\WindowsFireWall.suo

     目录          0  2009-03-16 14:14  WindowsFireWall\obj\Debug\TempPE

     目录          0  2009-03-16 15:33  WindowsFireWall\bin\Debug

     目录          0  2009-03-16 15:33  WindowsFireWall\obj\Debug

     目录          0  2009-03-16 14:14  WindowsFireWall\bin

     目录          0  2009-03-16 15:33  WindowsFireWall\obj

     目录          0  2009-03-16 14:14  WindowsFireWall\Properties

............此处省略4个文件信息

评论

共有 条评论