I used netsh for add my application to firewall as follows. Before I add it to the firewall, how to I know that the application has not been added to the firewall? because I do not want add my application to the firewall repeatedly.
ProcessStartInfo info = null;
try
{
using (Process proc = new Process())
{
string productAssembly = new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)).LocalPath + "\\" + this.ProductName + ".exe";
string args = string.Format(CultureInfo.InvariantCulture, "advfirewall firewall add rule name=\"{0}\" dir=in action=allow program=\"{1}\" enable=yes", this.ProductName, productAssembly);
info = new ProcessStartInfo("netsh", args);
proc.StartInfo = info;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}