Click here to Skip to main content
13,301,619 members (68,151 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 29 Nov 2012

Allow/Block a Program

, 29 Nov 2012
Rate this:
Please Sign up or sign in to vote.
This is a quick way to allow or disallow a program through the firewall.


This is a single method that allows/blocks an executable through the firewall. I wrote this method for one of my applications and decided to post here for everyone's good.


The idea here is to use the NetFwTypeLib located in (C:\windows\system32\FirewallAPI.dll) to add a rule that allows/blocks the executable with the specified path from establishing a connection of any type.

Using the Code

First, the FirewallAPI.dll must be added to references and NetFwTypeLib to usings.

Then, you can use the following method:

/// <summary>
/// Adds or removes a firewall rule.
/// </summary>
/// <param name="path">The path to the executable.</param>
/// <param name="d">The affected connection type.</param>
/// <param name="fwaction">Rule action.</param>
/// <param name="action">"Add (1) or 
/// remove (0) the specified rule."</param>
private void FWRule(string path, NET_FW_RULE_DIRECTION_ d, 
		NET_FW_ACTION_ fwaction, string action)
        INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
        firewallRule.Action = fwaction;
        firewallRule.Enabled = true;
        firewallRule.InterfaceTypes = "All";
        firewallRule.ApplicationName = path;
        firewallRule.Name = "CSwitch: " + Path.GetFileName(path);
        INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance
            firewallRule.Direction = d;
            if (action == "1") firewallPolicy.Rules.Add(firewallRule);
            else firewallPolicy.Rules.Remove(firewallRule.Name);
    catch (Exception ex) { MessageBox.Show(ex.Message, "ERROR"); }}} }   



This will block test.exe from making any outgoing connections.

Points of Interest

While writing this, I noticed that trying to use the same INetFwRule variable multiple times could throw a CATASTROPHIC FAILURE (Access Denied) exception.


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


About the Author

Hesham Meneisi
Egypt Egypt
I have taken programming as a hobby ever since I was a child. I'm currently a Computer & Communication BSc undergraduate.

You may also be interested in...


Comments and Discussions

QuestionThank You Pin
Tim Tatum7-May-15 10:21
memberTim Tatum7-May-15 10:21 
QuestionIf you are getting a "Value does not fall within the expected range." compile error Pin
hooger00720-Jun-14 13:12
memberhooger00720-Jun-14 13:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 30 Nov 2012
Article Copyright 2012 by Hesham Meneisi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid