如何使用C#代码转换防火墙 [英] How to turn of firewall using C# code

查看:86
本文介绍了如何使用C#代码转换防火墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我正在开展一个网络项目。所以在这个项目中我使用套接字将数据从一个系统传送到另一个系统。为此,必须关闭防火墙,否则它不会接受一个系统发送到另一个系统的数据。为此

我希望在我的应用程序启动时关闭防火墙,并在应用程序关闭时打开它。那我怎么能这样做呢。我看到它可以使用注册码(regedit)完成,但我不知道如何使用C#访问注册码。

我尝试使用



Hello every one

I am working on a networking project . So in this project i am using sockets to carry data from one system to another . For this the "Fire wall " has to be turn Off else it wont accept data send by one system to another one . For that
i want to turn off firewall when my application starts and turn on it when application is closed . So how can i do it . I saw it net it can be done using register key (regedit) but i don't know to how to access register key using C# .
I tried using

RegistryKey reg = new RegistryKey();





并且被这条路径吞没



and fallowed by this path

//HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\WindowsFirewall\DomainProfil\EnableFirewall=1 





,但它显示错误,所以任何人都可以帮我解决问题。即使在代码项目中我也看到了相同但代码是用Visual C ++编写的



but it is showing error so can anybody can help me how to solve it . Even in code project i saw the same but the code is written in visual C++

推荐答案

http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows -firewall-settings-with-c.aspx [ ^ ]


1)首先记得添加app.manifest(项目菜单 - >在项目中添加新项目>应用程序清单文件)和

2)添加
1) first remember to add "app.manifest"(Project menu->Add New Item->Application Manifest file) in project and
2) add
<requestedexecutionlevel level="requireAdministrator" uiaccess="false" />

</requestedPrivileges>

标签之前。

" tag.

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the
            requestedExecutionLevel node with one of the following.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            Specifying requestedExecutionLevel node will disable file and registry virtualization.
            If you want to utilize File and Registry Virtualization for backward
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>





3)现在在.cs文件中



4)使用System.Diagnostics添加库文件



3) now in .cs file

4) add library file

using System.Diagnostics;



5)启用系统防火墙


5) enable firewall of system

private void btnfire_Click(object sender, EventArgs e)
        {
            try
            {
                Process proc = new Process();
                string top = "netsh.exe";
                proc.StartInfo.Arguments = "Firewall set opmode enable";
                proc.StartInfo.FileName = top;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                proc.WaitForExit();
                MessageBox.Show("Enable");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error");
            }
        }





6)禁用系统防火墙





6)disable firewall of system

private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                Process proc = new Process();
                string top = "netsh.exe";
                proc.StartInfo.Arguments = "Firewall set opmode disable";
                proc.StartInfo.FileName = top;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                proc.WaitForExit();
                MessageBox.Show("Disable");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error");
            }
        }


这篇关于如何使用C#代码转换防火墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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