C# - 使用c#禁用防火墙时显示错误 [英] C# - Showing Errors while disabling firewall using c#

查看:70
本文介绍了C# - 使用c#禁用防火墙时显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Windows防火墙的状态。我成功了。并且还能够打开/启用关闭/禁用防火墙。



但是当我使用我的代码禁用防火墙时它显示此错误



http://i.stack.imgur.com/hxNHT.jpg [ ^ ]

< br $> b $ b请告诉我怎样才能实现。



代码在这里



I am trying to get status of windows firewall. I am successful in doing so. and also able to ON/ Enable the OFF/ Disabled Firewall.

But when i am Disabling the firewall using my code it is showing this error

http://i.stack.imgur.com/hxNHT.jpg[^]

Please tell me how could i make it possible.

Code is here

private void toggleButton1_Checked(object sender, RoutedEventArgs e)
   {
       toggleButton1.Content = "On";
       toggleButton1.Background = Brushes.LawnGreen;
       INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
       mgr.LocalPolicy.CurrentProfile.FirewallEnabled = true;
   }

   private void toggleButton1_Unchecked(object sender, RoutedEventArgs e)
   {
       toggleButton1.Content = "Off";
       toggleButton1.Background = Brushes.Red;
       INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
       mgr.LocalPolicy.CurrentProfile.FirewallEnabled=false;
   }



谢谢


Thank you

推荐答案

首先我认为你只会要求提升。我很快写了一个应用来测试这个理论。

以普通用户身份运行应用程序,我得到了同样的错误。然后我以管理员身份重新启动了IDE并再次尝试。这次我收到错误尚未实施。这是使用.Net4 .....

为什么微软说尚未实现是超出我的。



问题是,为什么你需要禁用防火墙,因为这首先会破坏防火墙的目的。但是你可以在防火墙上添加一个例外。



这是我使用的一段代码:



First I thought that you would only require elevation. I quickly wrote an application to test this theory.
Running the application as normal user, I got the same error. I then restarted my IDE as an administrator and tried again. This time I got the error "Not Yet Implemented". This was using .Net4.....
Why Microsoft says not yet implemented is beyond me.

Question is, why do you need to disable the firewall as this defeats the purpose of the firewall in the first place. You can however add an exception to the firewall.

Here is a piece of my code that I use:

public void OpenPort(string Name, int Port, NET_FW_IP_PROTOCOL_ protocol)
{
       INetFwMgr fwMgr = (INetFwMgr)getInstance("INetFwMgr");
       INetFwPolicy fwPolicy = fwMgr.LocalPolicy;
       INetFwProfile fwProfile = fwPolicy.CurrentProfile;
       INetFwOpenPorts openports = fwProfile.GloballyOpenPorts;
       INetFwOpenPort openport = (INetFwOpenPort)getInstance("INetOpenPort");
       openport.Port = Port;
       //.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP
       openport.Protocol = protocol;
       openport.Name = Name + Port; //The description that will be displayed in windows firewall.
       openports.Add(openport);
       fwMgr = null;
       fwPolicy = null;
       fwProfile = null;
       openports = null;
       openport = null;
   }


您应该使用Policy2互操作而不是防火墙管理器互操作。这将允许您将防火墙设置为启用等于false。
You should be using the Policy2 interop instead of the Firewall Manager interop. That will allow you to set the firewall to enabled equal false.


这篇关于C# - 使用c#禁用防火墙时显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆