2012-10-31 30 views
2

可能重複:在我的解決方案
Programmatically add an application to Windows Firewall編程添加一個Windows服務Windows防火牆(安裝期間)

我有一個Windows服務項目和安裝程序安裝此服務 如何我可以在安裝期間將此服務添加到Windows防火牆。

+0

根據部署項目。但是這裏是爲WiX的一個解決方案(http://wix.sourceforge.net/manual-wix3/firewall_xsd_firewallexception.htm) – Patrick

+0

你真的應該跟你的問題的更多細節展開..你有什麼嘗試?你使用什麼類型的「安裝程序」? – Patrick

+1

我不認爲這確實重複,其他問題是關於ClickOnce安裝程序。 – weston

回答

4

假設我們使用的是Visual Studio Installer->Setup Project - 在安裝的程序集中需要一個像這樣的安裝程序類,然後確保在安裝階段爲「主要輸出」添加自定義操作。

using System.Collections; 
using System.ComponentModel; 
using System.Configuration.Install; 
using System.IO; 
using System.Diagnostics; 

namespace YourNamespace 
{ 
    [RunInstaller(true)] 
    public class AddFirewallExceptionInstaller : Installer 
    { 
     protected override void OnAfterInstall(IDictionary savedState) 
     { 
      base.OnAfterInstall(savedState); 

      var path = Path.GetDirectoryName(Context.Parameters["assemblypath"]); 
      OpenFirewallForProgram(Path.Combine(path, "YourExe.exe"), 
            "Your program name for display"); 
     } 

     private static void OpenFirewallForProgram(string exeFileName, string displayName) 
     { 
      var proc = Process.Start(
       new ProcessStartInfo 
        { 
         FileName = "netsh", 
         Arguments = 
          string.Format(
           "firewall add allowedprogram program=\"{0}\" name=\"{1}\" profile=\"ALL\"", 
           exeFileName, displayName), 
         WindowStyle = ProcessWindowStyle.Hidden 
        }); 
      proc.WaitForExit(); 
     } 
    } 
} 
+0

謝謝weston,但是你是什麼意思「YourExe.exe」,你的意思是完整路徑+ exe文件名 – tito11

+0

@tito11 - 對不起,是的,你確實需要路徑,請看代碼的修改 – weston

+0

yes謝謝Weston – tito11