3

重複の可能性:
プログラムによってアプリケーションを Windows ファイアウォールに追加する

私のソリューションでは、このサービスをインストールするための Windows サービス プロジェクトとインストーラーがあります。インストール中にこのサービスを Windows ファイアウォールに追加する方法を教えてください。

4

1 に答える 1

8

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();
        }
    }
}
于 2012-10-31T13:27:56.150 に答える