Programatically Add Exceptions to a Windows Firewall Using C# | CodeGuru

Programatically Add Exceptions to a Windows Firewall Using C#

Programatically Add Exceptions to the Windows Firewall Using C# I think many of us have had, at some point, problems configuring SQL Server 2005 and received the following error message: “Microsoft SQL Native Client: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 30, 2007
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Programatically Add Exceptions to the Windows Firewall Using C#

I think many of us have had, at some point, problems configuring SQL Server 2005 and received the following error message:

“Microsoft SQL Native Client: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.”

To fix this problem, you need to enable a remote connection, turn on the SQL Server Browser Service, SQL Server, and SQL server Browser services to Firewall Exceptions. If you want more information about the first two matters, see http://support.microsoft.com/kb/914277.

My problem started when I had to add exceptions to a Windows Firewall programmatically. I really didn’t like to modify Registry keys, but at that moment and circumstances, it was the only solution.

It is an observation to make at this point: You need to add exceptions for standard and domain profiles.

First of all, you need to include:

using Microsoft.Win32;
using System.Collections;

Declare variables:

private const string keyToSearchStandardProfile =
   @"SYSTEMCurrentControlSetServicesShare";
private const string keyToSearchDomainProfile =
   @"SYSTEMCurrentControlSetServicesSharedAccessParametersFireWall";

//location to this exe files
private const  string applicationGeneralPath =
   @"C:Program FilesMicrosoft SQL Server";
private const string sqlbrowserPath = @"90Sharedsqlbrowser.exe";
private const string sqlservPath = @"MSSQL.1MSSQLBinnsqlservr.exe";

In both cases, I look whether these files are already added to the Windows Firewall exceptions and, only if they aren’t, I add them.

  1. Add to standard profile:
  2. private void AddExceptionsToStandardDomain()
    {
       string keyValue;
    
       //add to standard profile if does not exist
       RegistryKey key =
       Registry.LocalMachine.OpenSubKey
          (keyToSearchStandardProfile, true);
    
       // add sqlservr
       keyValue = applicationGeneralPath +
          @"MSSQL.1MSSQLBinnsqlservr.exe:*:Enabled:sqlservr";
    
       //check if key exists
       bool appAlreadyExist =
          new ArrayList(key.GetValueNames()).Contains(keyValue);
       if (!appAlreadyExist)
          key.SetValue(applicationGeneralPath + sqlservPath,
                       keyValue);
    
       //add sqlbrowser
       keyValue = applicationGeneralPath +
          @"90Sharedsqlbrowser.exe:*:Enabled:sqlbrowser";
       appAlreadyExist =
          new ArrayList(key.GetValueNames()).Contains(keyValue);
       if (!appAlreadyExist)
          key.SetValue(applicationGeneralPath + sqlbrowserPath,
                       keyValue);
       key.Close();
    
    }
    
  3. Add to domain profile:
  4. public static void AddExceptionsToDomainProfile()
    {
    
       string keyValue;
    
       //add to domain profile
       RegistryKey key =
          Registry.LocalMachine.OpenSubKey
             (keyToSearchDomainProfile, true);
    
       //sqlservr
       keyValue = applicationGeneralPath +
          @"MSSQL.1MSSQLBinnsqlservr.exe:*:Enabled:sqlservr";
       bool appAlreadyExist =
          new ArrayList(key.GetValueNames()).Contains(keyValue);
       if (!appAlreadyExist)
          key.SetValue(applicationGeneralPath+sqlservPath,
                       keyValue);
    
       //sqlbrowser
       keyValue = applicationGeneralPath +
          @"90Sharedsqlbrowser.exe:*:Enabled:sqlbrowser";
       appAlreadyExist =
          new ArrayList(key.GetValueNames()).Contains(keyValue);
       if (!appAlreadyExist)
          key.SetValue(applicationGeneralPath+sqlbrowserPath,
                       keyValue);
       key.Close();
    
    }
    

I hope this article is helpful to you. If you have any questions or suggestions, please do not hesitate to contact me.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.