Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
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

C#
RegistryKey reg = new RegistryKey();


and fallowed by this path
C#
//HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\WindowsFirewall\DomainProfil\EnableFirewall=1 


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++
Posted
Updated 1-Apr-16 9:33am
Comments
Member 12203827 6-Jun-16 2:52am    
This worked perfectly for me!

I have a question, my users tell me they get not connected to server error, despite they are connected to internet. I'm not sure if the firewall is blocking access to internet or the anti-virus. In this case, I will disable their firewall when the application starts and re-enable the firewall when the application closes.

I was thinking it won't be necessary if my program gets added into firewall exception list.

Please tell me a way to do this.


Thanks.

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" />
before "
</requestedPrivileges>
" tag.
XML
<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) now in .cs file

4) add library file
using System.Diagnostics;

5) enable firewall of system
C#
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)disable firewall of system

C#
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");
            }
        }
"
 
Share this answer
 
v3
Comments
Member 11546513 21-Apr-15 8:05am    
can you share your source code..? actually i wanted enable windows firewall port programmatically in c# asp.net
My email id:kamalahanchinal58@gmail.com

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900