2013-03-21 20 views
6

Uygulamamı aşağıdaki gibi güvenlik duvarına eklemek için netsh kullanmıştım. Güvenlik duvarına eklemeden önce, uygulamanın güvenlik duvarına eklenmediğini nasıl anlarım? uygulamanızı tekrar tekrar güvenlik duvarına eklemek istemiyorum.Uygulamamın güvenlik duvarına eklenmediğini nasıl bilebilirim?

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

Kontrol: http://stackoverflow.com/questions/113755/programmatically-add-an-application-to-windows-firewall –

+0

@TheGreatCO Hem farklı bir soru vardır, belirtti ve bu hakkındadır –

+0

algılamayla ilgilidir. Tipik olarak bunu yükleme sırasında bir kez yaparsınız - bu durumda, onu güvenlik duvarına eklemeniz (ve kaldırma sırasında çıkarmanız) gerekmez. –

cevap

1

TheGreatCO, Thank You. Denedim ve işe yaradı. Bu yazı dışarı

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; 
} 
İlgili konular