部署 NetFwTypeLib 来管理 Windows 防火墙

问题描述 投票:0回答:4

我的 Windows 服务需要在 Windows 防火墙中创建/删除某些规则。为此,我通过 COM 与

NetFwTypeLib
中的
<windows>\system32\hnetcfg.dll
进行交互。它在我的 64 位 Windows 7 机器上运行得很好,但在另一台 64 位 Windows 7 机器上测试会抛出以下错误: Service cannot be started. System.IO.FileNotFoundException: Could not load file or assembly 'Interop.NetFwTypeLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

我有一种感觉,如果我在我的应用程序中嵌入并安装程序集,我会在不同版本的 Windows 以及 32 位和 64 位之间遇到问题。

如何解决缺少程序集部署问题?


编辑:

对于除 4.0 之外的任何目标框架来说,这似乎都是 VS2010 问题。有人能解决这个问题吗?

c# deployment assemblies filenotfoundexception windows-firewall
4个回答
14
投票

使用 NetFwTypeLib; // 添加参考 %SystemRoot%\System32\FirewallAPI.dll


1
投票
System32

版本的

DLL
,将其复制到您的文件夹中并从那里调用它。据我所知,我认为 DLL 不应该与不同位的计算机发生冲突,但如果它们发生冲突,那么只需从 32 位计算机获取不同的 DLL,并分别下载
x64
x86
。祝你好运!

编辑:另外,我在 3.5 或更低版本的

VS2010

中编程时遇到了一些麻烦。尝试获取

Visual C# Express 2008
的版本并尝试使用它(通常通过降级 .net 版本来修复很多错误)
    


1
投票
interop.NetFwTypeLib.dll

移动到我正在工作的目录中。这似乎解决了我的问题。希望有帮助

    


0
投票

根据 Microsoft 文档:

https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2013/ff737187(v=vs.120)

NetFwTypeLib

使用的接口也在命名空间中定义:

Microsoft.TeamFoundation.Common

组装:

Microsoft.TeamFoundation.Common (in Microsoft.TeamFoundation.Common.dll)

那么你只需要以下功能:

INetFwMgr iNetMgr = null; try{ Type tNetMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr") iNetMgr = (INetFwMgr)Activator.CreateInstance(tNetMgr); }

现在它似乎是程序集可移植的,不需要对 dll 进行任何复制,虽然我还没有测试它,但编译器没有对此发出任何噪音。

© www.soinside.com 2019 - 2024. All rights reserved.