首页   Vista新闻   Vista应用   Vista技巧   Vista下载   Vista行情   Vista桌面主题   各种下载   测试栏目
Vista探索者
Vista探索者首页>Vista应用>Vista设置> 在Vista中编程控制防火墙设定
  • 进入探索者首页
  • 在Vista中编程控制防火墙设定
    来源:Vista探索者作者:vistause 发布时间:2008-06-05 阅读次数

    在编程控制防火墙前先要有个前提,就是你必须是管理员权限, 这样本例De程序才能用"Run as administrator"De方式运行,并成功修改. 如果你本身就是用Administrator这个用户登录De话,直接运行就行了. 建议最好在这个用户下来调试程序.

    在编程控制防火墙前先要有个前提,就是你必须是管理员权限, 这样本例De程序才能用"Run as administrator"De方式运行,并成功修改. 如果你本身就是用Administrator这个用户登录De话,直接运行就行了. 建议最好在这个用户下来调试程序.

      本程序只是个初始De例子,里面De功能只开发了一部分,各位有兴趣De话可以继续深入运用. 像VistaDe防火墙就比较Bt,除了基本设定外,在"Control Panel\Administrative Tools\Windows Firewall with Advanced Security" 还有高级设定,好像用程序都可控制.

      FireWallManager 程序主要功能有

      1. public void FireWallTrigger( bool enable ) //开关防火墙. 貌似在Vista里面有问题,XP sp2好像可以. 但是用INetFwPolicy2.set_FirewallEnabledDe方法De话,Vista也能搞定.

      2. public void FireWallService( string name, bool enable ) //开关防火墙服务程序,一般里面De File and Printer Sharing 服务比较有用.

      3. public bool AddPort( string portName, int portNumber, string protocol ) // 开启一个端口.

      4. public bool RemovePort( int portNumber, string protocol ) //删除开启De端口

      5. public bool AddAplication( string discriptionName, string fileName ) //开启放行应用程序

      6. public bool RemoveApplication( string fileName ) // 关闭放行De应用程序.

      里面还有个 protected Object getInstance( String typeName ) 本来是用CLSID来实例化那些接口De,后来发现ProgID其实更简单,不需要查,里面有个规律,只需把接口DeINet删掉就是ProgID了. 如 INetFwOpenPort port = ( INetFwOpenPort )Activator.CreateInstance( Type.GetTypeFromProgID( "HNetCfg.FwOpenPort" ) ); 中 INetFwOpenPort 与 FwOpenPort.

      首先,创建一个Console程序,在程序中添加引用,在COM对象中找到"NetFwTypeLib" ,添加即可. 防火墙主要是靠这个对象操作De. 貌似不止Vista, Xp也是一样De.核心程序如下:

    FireWallManager.cs
    using System;
    using System.Collections.Generic;
    using System.Text;
    using NetFwTypeLib;

    namespace FirewallManager
    {
    class FwManager
    {
    private INetFwMgr NetFwMgr;
    private INetFwProfile NetFwProfile;
    private INetFwPolicy2 NetFwPolicy2; //this interface contains lots of usefull functions.

    public FwManager()
    {
    //Create Com Object
    //Type NetFwMgrType = Type.GetTypeFromCLSID( new Guid( "{304CE942-6E39-40D8-943A-B913C40C9CD4}" ) );
    Type NetFwMgrType = Type.GetTypeFromProgID( "HNetCfg.FwMgr" );
    object NetFwMgrObject = Activator.CreateInstance( NetFwMgrType );
    NetFwMgr = ( INetFwMgr )NetFwMgrObject;
    NetFwProfile = NetFwMgr.LocalPolicy.CurrentProfile;

    Type NetFwPolicy2Type = Type.GetTypeFromProgID( "HNetCfg.FwPolicy2" );
    object NetFwPolicy2Object = System.Activator.CreateInstance( NetFwPolicy2Type );
    NetFwPolicy2 = ( INetFwPolicy2 )NetFwPolicy2Object;
    }

    public void ShowInfo()
    {
    switch( NetFwProfile.Type )
    {
    case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN:
    Console.WriteLine( "Network Profile Type1: " "Domain" );
    break;
    case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD:
    Console.WriteLine( "Network Profile Type1: " "Standard" );
    break;
    case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_CURRENT:
    Console.WriteLine( "Network Profile Type1: " "Current" );
    break;
    case NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_TYPE_MAX:
    Console.WriteLine( "Network Profile Type1: " "Max" );
    break;
    }
    switch( ( NET_FW_PROFILE_TYPE2_ )NetFwPolicy2.CurrentProfileTypes )
    {
    case NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN:
    Console.WriteLine( "Network Profile Type2: " "Domain" );
    break;
    case NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE:
    Console.WriteLine( "Network Profile Type2: " "Private" );
    break;
    case NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC:
    Console.WriteLine( "Network Profile Type2: " "Public" );
    break;
    case NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL:
    Console.WriteLine( "Network Profile Type2: " "All" );
    break;
    }
    Console.WriteLine( "Firewall Enabled: " NetFwProfile.FirewallEnabled );
    Console.WriteLine( "Exceptions Not Allowed: " NetFwProfile.ExceptionsNotAllowed );
    Console.WriteLine( "Notifications Disabled: " NetFwProfile.NotificationsDisabled );
    //Console.WriteLine("UnicastResponsestoMulticastBroadcastDisabled: " NetFwProfile.UnicastResponsestoMulticastBroadcastDisabled);
    //Remote Admin
    INetFwRemoteAdminSettings RASettings = NetFwProfile.RemoteAdminSettings;
    Console.WriteLine( "Remote Administration Enabled: " RASettings.Enabled );

    上一篇:Vista系统启动加速和vista系统运行加速  
    下一篇:微软安全专家Steve Riley:谁需要杀毒软件呢?

    共3页: 上一页 1 [2] [3] 下一页
    关注此文读者还看过
    • ·将XP打扮的和Vista一模一样!
      ·Windows Vista不能连接网络?vista999教你快
      ·vista文件夹颜色随心换,随你换,更美观!
      ·用Vista后,还需要防火墙吗?
      ·电脑安装Vista操作系统的十大要素
      ·Vista如何找出木马?任务管理器隐藏的秘密!
    最新更新
    • ·将XP打扮的和Vista一模一样!
      ·Windows Vista不能连接网络?vista999教你快
      ·vista文件夹颜色随心换,随你换,更美观!
      ·用Vista后,还需要防火墙吗?
      ·电脑安装Vista操作系统的十大要素
      ·Vista如何找出木马?任务管理器隐藏的秘密!
    本站推荐
    网站精华
    Vista中U盘当内存用能达到内存的速度吗
    Vista中U盘当
    Windows Vista32位和64位有什么区别
    Windows Vist
    体验动感 Vista下超级Mac OSX模仿秀
    体验动感 Vis
    • ·东芝公布四款新本 最轻型号仅
    • ·富士通公布E8410新本 可配T930
    • ·军用级别 Getac推新款轻便坚固
    • ·海尔A650以高贵血统博“超值霸
    • ·富士通P8010本下月开售 配SL71
    文章关注度排行
    · 【原创】手动修改使Vista使用默认用户名
    · 如何把Windows Vista系统默认的雅黑字体
    · MicrosoftAppLocale,Vista系统彻底解决
    · Vista两天吃掉20G空间?清理还原点或关
    · 【原创】如何开启Windows Vista Aero效
    · 【原创】新手学Vista系统中IP地址设置方
    · 【原创】Windows Vista中设置麦克风
    · Vista下硬盘保护技巧 Vista优化
    · 【原创】Windows Vista轻松修改启动程序
    · Vista下面调整输入法顺序和设置输入法快
    热点推荐
    Vista中U盘当内存用能达到内存的速度吗
    Vista中U盘当内存用能达
    Windows Vista32位和64位有什么区别
    Windows Vista32位和64
    体验动感 Vista下超级Mac OSX模仿秀
    体验动感 Vista下超级Ma
    对比分析 XP与Vista系统游戏性能评比
    对比分析 XP与Vista系统
    体验Windows Vista优化系统恢复管理
    体验Windows Vista优化
    Windows XP版侧边栏试用报告及下载
    Windows XP版侧边栏试用
    vista探索者
    ©2006 Microsoft and its suppliers. 保留所有权利。 使用条款 广告服务 隐私声明 反垃圾邮件策略
    探索者ICP证B2-20030003号