通过编程管理windows 防火墙

作者:肖波
    最近项目中需要通过程序自动设置windows 防火墙,查了一下资料,可以通过命令行netsh firewall来实现。封装了一个类来实现对允许放开的程序(Allowed program)进行管理。管理其他内容比如放开端口等方法是类似的。
    程序中用到一个公共类 RunProcess,这个类可从我的另一篇文章 一个C#写的调用外部进程类 获得

namespace  WinFirewall
{

    
public enum TScope
    
{
        ALL,
        SUBNET,
        CUSTOM,
    }


    
public enum TMode
    
{
        ENABLE,
        DISABLE,
    }


    
/// <summary>
    
/// Manage the allowed program with the Windows Firewall.
    
/// </summary>

    public class AllowedProgram
    
{
        
Set AllowedProgram Help

        
private field

        
public property

        
public method
    }

}


调用的相关例程

         private   void  buttonSetAllowProgram_Click( object  sender, EventArgs e)
        
{
            
try
            
{
                AllowedProgram allowedProgram 
= new AllowedProgram();
                allowedProgram.Program 
= textBoxProgramFilePath.Text.Trim();

                
if (checkBoxEnable.Checked)
                
{
                    allowedProgram.Mode 
= TMode.ENABLE;
                }

                
else
                
{
                    allowedProgram.Mode 
= TMode.DISABLE;
                }


                allowedProgram.Scope 
= (TScope)comboBoxScope.SelectedItem;

                allowedProgram.Address 
= textBoxAddress.Text.Trim();

                allowedProgram.Set();

                MessageBox.Show(
"OK""Information", MessageBoxButtons.OK);
            }

            
catch (Exception e1)
            
{
                MessageBox.Show(e1.Message, 
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }


        
private   void  buttonDelAllowProgram_Click( object  sender, EventArgs e)
        
{
            
try
            
{
                AllowedProgram allowedProgram 
= new AllowedProgram();
                allowedProgram.Program 
= textBoxProgramFilePath.Text.Trim();
                allowedProgram.Delete();

                MessageBox.Show(
"OK""Information", MessageBoxButtons.OK);
            }

            
catch (Exception e1)
            
{
                MessageBox.Show(e1.Message, 
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值