以编程方式将Windows服务添加到Windows防火墙(在安装过程中) [英] Programmatically add an windows service to Windows Firewall (During Installation)
本文介绍了以编程方式将Windows服务添加到Windows防火墙(在安装过程中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
以编程方式将应用程序添加到Windows防火墙
Possible Duplicate:
Programmatically add an application to Windows Firewall
在我的解决方案中,我有一个Windows服务项目和安装程序来安装此服务 我如何在安装过程中将此服务添加到Windows防火墙.
in my solution i have an windows service project and installer to install this service How i can add this service to Windows Firewall During Installation.
推荐答案
假设我们使用的是Visual Studio Installer->Setup Project
-您需要在正在安装的程序集中安装一个此类的安装程序类,然后确保添加自定义操作在安装阶段获取主要输出".
Assuming we're using a Visual Studio Installer->Setup Project
- You need an installer class like this inside an assembly that's being installed, and then make sure you add a custom action for the "Primary output" in the install phase.
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();
}
}
}
这篇关于以编程方式将Windows服务添加到Windows防火墙(在安装过程中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文