Hey Friends, today I’ll make a short blog post brief and straight to the point. Window’s firewall is basically a network security system which makes sure incoming and outgoing traffic on your computer is safe. As software developers we might face situations were by, accessing the windows firewall is required by our software. Most often this is done manually. But it can also be done using good old code. This blog post is just about how to allow your app through the firewall with C# code.

How Allow Your App through the firewall with C#

Recently, I’ve faced a situation were I had to allow an app through Windows Firewall with C# (Both the private and public profile of the firewall). At first, the app was added manually after installation, but I automated that process with C#. After a few Google searches, I came quickly accross several solutions. Although some were working, they only allowed my app through the “Public profile”. Which is not enough. After researching a bit deeper, I came accross a solution to this. This blog post is about accomplishing the following.

  • Adding your app to the public and private profiles of the Windows Firewall
  • Removing your app from the firewall when needed.

If you like this post, you can chose to follow me on Twitter or Github and subscribe to this page’s notifications to stay updated with new articles or like my page on Facebook.

Adding to the Firewall

First you need to reference external libraries provided by Microsoft to access your firewall. These are: hnetcfg.dll and FirewallAPI.dll These are not on nuget, they are found your PC at the following locations. “C:\\Windows\System32\“. These libraries contain helpers to allow you to manipulate firewall rules. You should note that, the app should be run as administrator to function properly.

To add your app’s rule to your firewall profile, it is very easy.

To remove your app, is just as simple.

Conclusion

With this, you will be able to access your fire wall and configure your fire wall to allow your application to the private and public profile. You might also be interested by this blog post about detecting global mouse hooks in C#.

If you find this article useful, please follow me on Twitter,  Github, Linkedin, or like my Facebook page to stay updated.
Follow me on social media and stay updated

Refferences

http://web.archive.org/web/20070707110141/http://www.dot.net.nz/Default.aspx?tabid=42&mid=404&ctl=Details&ItemID=8

https://stackoverflow.com/questions/15409790/adding-an-application-firewall-rule-to-both-private-and-public-networks-via-win7

Follow me on social media and stay updated

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.