繁体   English   中英

我如何知道我的应用程序尚未添加到防火墙?

[英]How do I know my application has not been added to the firewall?

我使用netsh将我的应用程序添加到防火墙,如下所示。 在将其添加到防火墙之前,如何知道应用程序尚未添加到防火墙? 因为我不想反复将我的应用程序添加到防火墙。

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);
}

TheGreatCO,谢谢。 我已经尝试过它并且有效。

private bool isFirewallEnabled()
{
    ProcessStartInfo info = null;
    string result = string.Empty;
    try
    {
       using (Process proc = new Process())
       {
           string args = string.Format(CultureInfo.InvariantCulture, "advfirewall firewall show rule name=\"{0}\"", this.ProductName);
           info = new ProcessStartInfo("netsh", args);
           proc.StartInfo = info;
           proc.StartInfo.UseShellExecute = false;
           proc.StartInfo.CreateNoWindow = true;
           proc.StartInfo.RedirectStandardOutput = true;
           proc.Start();

           while ((result = proc.StandardOutput.ReadLine()) != null)
           {
               if (result.Replace(" ", String.Empty) == "Enabled:Yes")
               {
                   return true;
               }
            }
        }
    }
    catch (Exception ex)
    {
       MessageBox.Show(ex.Message);
    }
    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM