static bool AddFirewallRule()
{
//accomplished via shelling out to netsh.exe with relevant arguments.
Process proc = new Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.UseShellExecute = false; //the process should be created directly from this app.
proc.StartInfo.RedirectStandardError = true; //allowed since we set UseShellExecute to false
proc.StartInfo.FileName = "netsh.exe"; //would include the folder name if not in system path
proc.StartInfo.Arguments = "advfirewall firewall add rule name=\"Banned IP Addresses\" dir=in action=block description=\"IPs detected from Security Event Log with more than 10 failed attempts a day\" enable=yes profile=any localip=any protocol=any interfacetype=any";
proc.StartInfo.CreateNoWindow = true;
string errstr = "";
try
{
OutputMsg("Starting netsh.exe to add firewall rule");
proc.Start();
errstr = proc.StandardError.ReadToEnd();
proc.WaitForExit();
}
catch (Exception e)
{
OutputMsg("Unable to add firewall rule. Error starting process for netsh.exe" , e.ToString());
proc.Dispose();
return false;
}
if (errstr != "")
{
OutputMsg("Suspicious output from netsh.exe:\n\t" + errstr +
"\n*** Check to verify that the update was processed correctly. ***");
}
proc.Dispose();
return true;
}