dcsimg

Programatically Add Exceptions to a Windows Firewall Using C#

WEBINAR:
On-Demand

Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


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 =
   @"SYSTEM\CurrentControlSet\Services\Share";
private const string keyToSearchDomainProfile =
   @"SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FireWall";

//location to this exe files
private const  string applicationGeneralPath =
   @"C:\Program Files\Microsoft SQL Server";
private const string sqlbrowserPath = @"\90\Shared\sqlbrowser.exe";
private const string sqlservPath = @"\MSSQL.1\MSSQL\Binn\sqlservr.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.1\MSSQL\Binn\sqlservr.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 +
          @"\90\Shared\sqlbrowser.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.1\MSSQL\Binn\sqlservr.exe:*:Enabled:sqlservr";
       bool appAlreadyExist =
          new ArrayList(key.GetValueNames()).Contains(keyValue);
       if (!appAlreadyExist)
          key.SetValue(applicationGeneralPath+sqlservPath,
                       keyValue);
    
       //sqlbrowser
       keyValue = applicationGeneralPath +
          @"\90\Shared\sqlbrowser.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.



About the Author

Daniela Ilea

.Net developer (MCP since september 2006) , passionate about mobile technologies. Lives in Timisoara, Romania

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date