- public class XPFireWall
- {
- /// <summary>
- /// Configuration du firewall windows
- /// </summary>
- /// <param name="protocol">Protocol de communication du port</param>
- /// <param name="port">Numero du port</param>
- /// <param name="mode">Mode de configuration</param>
- /// <param name="scope">Etendue</param>
- /// <param name="RuleName">Nom de la règle de configuration</param>
- /// <returns>Execution reussit</returns>
- public bool SetPort(Protocol protocol, int port, Mode mode, Scope scope, string RuleName)
- {
- bool _blnReussit = false;
-
- //Version de L'OS Minimum pour la configuration du firewall
- Version _versOSVersion = new Version(5, 1, 2600);
-
- if ((Environment.OSVersion.Version > _versOSVersion) || ((Environment.OSVersion.Version == _versOSVersion) && (int.Parse(Environment.OSVersion.ServicePack.Replace("Service Pack ", "")) >= 2)))
- {
- //Concaténation des parametres
- string _strParam = string.Concat(new object[] { "firewall set portopening ", protocol, " ", port, " ", RuleName, " ", mode, " ", scope });
-
- //Parametrage du process NETSH
- Process _procNetSH = new Process();
- _procNetSH.StartInfo.FileName = "netsh";
- _procNetSH.StartInfo.Arguments = _strParam;
- _procNetSH.StartInfo.CreateNoWindow = true;
- _procNetSH.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
- _procNetSH.StartInfo.UseShellExecute = false;
- _procNetSH.StartInfo.RedirectStandardOutput = true;
- _procNetSH.StartInfo.RedirectStandardError = true;
- try
- {
- //Execution du processus NETSH
- _procNetSH.Start();
- _procNetSH.WaitForExit(20000);
- if (_procNetSH.ExitCode == 0)
- {
- _blnReussit = true;
- }
- }
- catch
- {
- }
- finally
- {
- if (_procNetSH != null)
- {
- _procNetSH.Close();
- _procNetSH.Dispose();
- }
- }
- }
- return _blnReussit;
- }
-
- public enum Mode
- {
- Enable,
- Disable
- }
- public enum Profil
- {
- Current,
- Domain,
- Standard,
- All
- }
- public enum Protocol
- {
- TCP,
- UDP,
- ALL
- }
- public enum Scope
- {
- All,
- Subnet,
- Custom
- }
- }
public class XPFireWall
{
/// <summary>
/// Configuration du firewall windows
/// </summary>
/// <param name="protocol">Protocol de communication du port</param>
/// <param name="port">Numero du port</param>
/// <param name="mode">Mode de configuration</param>
/// <param name="scope">Etendue</param>
/// <param name="RuleName">Nom de la règle de configuration</param>
/// <returns>Execution reussit</returns>
public bool SetPort(Protocol protocol, int port, Mode mode, Scope scope, string RuleName)
{
bool _blnReussit = false;
//Version de L'OS Minimum pour la configuration du firewall
Version _versOSVersion = new Version(5, 1, 2600);
if ((Environment.OSVersion.Version > _versOSVersion) || ((Environment.OSVersion.Version == _versOSVersion) && (int.Parse(Environment.OSVersion.ServicePack.Replace("Service Pack ", "")) >= 2)))
{
//Concaténation des parametres
string _strParam = string.Concat(new object[] { "firewall set portopening ", protocol, " ", port, " ", RuleName, " ", mode, " ", scope });
//Parametrage du process NETSH
Process _procNetSH = new Process();
_procNetSH.StartInfo.FileName = "netsh";
_procNetSH.StartInfo.Arguments = _strParam;
_procNetSH.StartInfo.CreateNoWindow = true;
_procNetSH.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_procNetSH.StartInfo.UseShellExecute = false;
_procNetSH.StartInfo.RedirectStandardOutput = true;
_procNetSH.StartInfo.RedirectStandardError = true;
try
{
//Execution du processus NETSH
_procNetSH.Start();
_procNetSH.WaitForExit(20000);
if (_procNetSH.ExitCode == 0)
{
_blnReussit = true;
}
}
catch
{
}
finally
{
if (_procNetSH != null)
{
_procNetSH.Close();
_procNetSH.Dispose();
}
}
}
return _blnReussit;
}
public enum Mode
{
Enable,
Disable
}
public enum Profil
{
Current,
Domain,
Standard,
All
}
public enum Protocol
{
TCP,
UDP,
ALL
}
public enum Scope
{
All,
Subnet,
Custom
}
}