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.
- Add to standard profile:
- Add to domain profile:
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(); }
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.