新浦京81707con > 首页 > 操作防火墙,在Vista中编程控制防火墙设定

原标题:操作防火墙,在Vista中编程控制防火墙设定

浏览次数:166 时间:2019-11-27

关于C#操作防火墙,阻止程序联网,

//开启服务、开启防火墙 public void OpenFileWall() { // 1. 料定当前系统为XP或Win7 RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion"); var VersionName = rk.GetValue("ProductName").ToString(); rk.Close(); RegistryKey key; //获取防火墙服务名称 var ServicerName = ""; if (VersionName.Contains("XP")) { ServicerName = "SharedAccess"; key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\SharedAccess", true); } else { ServicerName = "MpsSvc"; key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\MpsSvc", true); } // 2. 确定防火墙运行项目是还是不是为禁止,若为禁绝设置其为自行 var StartIndex = key.GetValue("Start").ToString(); if (StartIndex == "4") { ProcessStartInfo objProInfo = new ProcessStartInfo(); objProInfo.FileName = "cmd.exe"; objProInfo.CreateNoWindow = false; objProInfo.WindowStyle = ProcessWindowStyle.Hidden; objProInfo.Arguments = "/c sc config " ServicerName " start= " "auto"; Process.Start(objProInfo); //挂起线程1s后开发银行服务 System.Threading.Thread.Sleep(1000); } key.Close(); // 3. 论断防火墙服务是还是不是运行 ServiceController sc = new ServiceController(ServicerName); if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending))) { sc.Start(); //挂起线程1s后展开防火墙 System.Threading.Thread.Sleep(1000); } // 4.开启防火墙 if (VersionName.Contains("XP")) { RegistryKey rekey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\斯坦dardProfile", true); var Enablefilewall = rekey.GetValue("EnableFirewall").ToString(); if (Enablefilewall == "0") { rekey.SetValue("EnableFirewall", 1); } rekey.Close(); } else { INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); // 启用或剥夺<高端安全Windows防火墙> - 专有配置文件的防火墙 firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_P凯雷德IVATE, true); // 启用或剥夺<高等安全Windows防火墙> - 公用配置文件的防火墙 firewallPolicy.set_FirewallEnabled(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, true); } } //当检查评定不满足条件,阻止访谈,只允许本程序运维 public void UnAllowOpenFW() { //决断系统归于xp照旧win7 using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion")) { var VersionName = rk.GetValue("ProductName").ToString(); if (VersionName.Contains("XP")) { // 1. 创立贰个证实程序类的实例 INetFwAuthorizedApplication Fwapp = (INetFwAuthorizedApplication)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication")); Fwapp.Name = "360安然无恙浏览器"; //Fwapp.Name = Application.ProductName; Fwapp.ProcessImageFileName = "C:\Documents and Settings\Administrator\Application Data\360se6\Application\360se.exe"; //Fwapp.ProcessImageFileName = Application.ExecutablePath; //是还是不是启用 Fwapp.Enabled = true; // 2. 创办firewall处理类的实例 ,增添程序到防火墙例外 INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(Fwapp); } else { // 1. 创制实例,阻止全部的出站连接 INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); //启用或剥夺<高档安全Windows防火墙> - 专有配置文件的出站连接 firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, NET_FW_ACTION_.NET_FW_ACTION_BLOCK); //启用或剥夺<高端安全Windows防火墙> - 公用配置文件的出站连接 firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, NET_FW_ACTION_.NET_FW_ACTION_BLOCK); // 2. 创设本程序出站准绳,只运维本程序的连天。 INetFwRule2 OutBoundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); OutBoundRule.Name = "360安然如故浏览器"; //OutBoundRule.Name = Application.ProductName; OutBoundRule.Description = "360逢凶化吉浏览器"; //程序路线 OutBoundRule.ApplicationName = "C:\Users\Administrator\AppData\Roaming\360se6\Application\360se.exe"; //OutBoundRule.ApplicationName = Application.ExecutablePath; OutBoundRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; OutBoundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; OutBoundRule.Enabled = true; // 3.增添出站法规 firewallPolicy.Rules.Add(OutBoundRule); } } } //检查测量检验满意条件,开启访谈 public void AllowOpenFW() { //判定系统归于xp照旧win7 using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion")) { var VersionName = rk.GetValue("ProductName").ToString(); if (VersionName.Contains("XP")) {

//贫乏方法 } else { // 1. 创办实例,允许具有程序的总是。 INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); //启用或剥夺<高端安全Windows防火墙> - 专有配置文件的出站连接 firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, NET_FW_ACTION_.NET_FW_ACTION_ALLOW); //启用或剥夺<高端安全Windows防火墙> - 公用配置文件的出站连接 firewallPolicy.set_DefaultOutboundAction(NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, NET_FW_ACTION_.NET_FW_ACTION_ALLOW); // 2. 剔除本程序的出站法规删除法规 INetFwRule2 OutBoundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")); OutBoundRule.Name = "360安然依旧浏览器"; //OutBoundRule.Name = Application.ProductName; firewallPolicy.Rules.Remove(OutBoundRule.Name); } } }

  

//开启服务、开启防火墙 public void OpenFile沃尔() { // 1. 论断当前系统为XP或Win7 RegistryKey rk = Registry.LocalMa...

那是本地精谋一些素材,一个通过访问当前操作系统的登记表来获取当前操作系统相关音信的小程序,仅供参考。

     在编制程序序调节制防火墙前先要有个前提,正是你必得是协会者权限, 那样品例的主次技巧用"Run as administrator"的不二等秘书技运营,并成功校正. 尽管你我就是用Administrator这几个顾客登入的话,直接运维就行了. 建议最棒在此个客户下来调节和测量检验程序.
     本程序只是个开头的例证,里面包车型大巴成效只开拓了后生可畏有的,各位有意思味的话能够继承浓郁应用. 像Vista的防火墙就相比较Bt,除了主导设定外,在"Control PanelAdministrative ToolsWindows Firewall with Advanced Security" 还应该有高档设定,好像用程序都可调节.
    FireWallManager 程序重要功能有
      1. public void FireWallTrigger( bool enable )  //按钮防火墙. 貌似在Vista里面有毛病,XP sp2好像能够. 不过用INetFwPolicy2.set_FirewallEnabled的办法的话,Vista也能化解.
      2.  public void Fire沃尔Service( string name, bool enable )  //按键防火墙服务程序,经常里面包车型地铁 File and Printer Sharing 服务相比较有用.
      3.  public bool AddPort( string portName, int portNumber, string protocol )   // 开启一个端口.
      4.  public bool RemovePort( int portNumber, string protocol )          //删除开启的端口
      5.  public bool AddAplication( string discriptionName, string fileName )   //开启放行应用程序
      6.  public bool RemoveApplication( string fileName )         // 关闭放行的选用程序.

 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Text;
 7using System.Windows.Forms;
 8using Microsoft.Win32;
 9
10namespace Reg4U
11{
12    public partial class Form1 : Form
13    {
14        public Form1()
15        {
16            InitializeComponent();
17        }
18
19        private void button1_Click(object sender, EventArgs e)
20        {
21            RegistryKey rk;
22            rk = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion");
23            string s= "当前操作系统版本:" rk.GetValue("ProductName").ToString();
24            s = s "rn" rk.GetValue("CSDVersion").ToString() ;
25            s = s "rn当前操作系统安装连串号:rn" rk.GetValue("ProductId").ToString();
26            s = s "rn当前系统版本号:" rk.GetValue("CurrentBuildNumber").ToString();
27            rk.Close();
28            textBox1.Text = textBox1.Text "rn" s;
29        }
30
31        private void Form1_Load(object sender, EventArgs e)
32        {
33            RegistryKey rk;
34            rk = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion");
35            string s = rk.GetValue("ProductName").ToString();
36            if (System.Text.RegularExpressions.Regex.IsMatch(s, "Windows 2000"))
37            {
38                textBox1.Text = "您的操作系统是2K,恭喜你,你的最近系统相符本软件的接纳!";
39            }
40            rk.Close();
41        }
42    }
43}

其间还会有个 protected Object getInstance( String typeName ) 本来是用CLSID来实例化那个接口的,后来开采ProgID其实更简短,不供给查,里面有个规律,只需把接口的INet删掉正是ProgID了. 如 INetFwOpenPort port = ( INetFwOpenPort )Activator.CreateInstance( Type.GetTypeFromProgID( "HNetCfg.FwOpenPort" ) ); 中 INetFwOpenPort 与 FwOpenPort.
     首先,创建叁个Console程序,在程序中增加引用,在COM对象中找到"NetFwTypeLib" ,加多就能够. 防火墙首借使靠这些目的操作的. 貌似不唯有Vista, Xp也是相仿的。大旨程序如下:

本文由新浦京81707con发布于首页,转载请注明出处:操作防火墙,在Vista中编程控制防火墙设定

关键词: 新浦京81707con Network

上一篇:clientLeft之间的区别

下一篇:没有了