如何在.NET核心2.1中使用NetFwTypeLib [英] How to use NetFwTypeLib in .NET core 2.1

查看:228
本文介绍了如何在.NET核心2.1中使用NetFwTypeLib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:

我使用C#Version 7.3进行了C#项目;我使用PowerShell设置防火墙规则,其名称类似于"My_Firewall_Rule"。

I had a C# project using C# Version 7.3; and I setup a firewall rule using PowerShell with a name like "My_Firewall_Rule".

我添加了对Interop.NetFwTypeLib.dll的引用,以下代码有效:

I added Reference to Interop.NetFwTypeLib.dll, and the following code works:

using NetFwTypeLib;

bool firewall_rule_found = false;
var firewallRule = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
foreach (INetFwRule rule in firewallRule.Rules)
{
    if (rule.Name == My_Firewall_Rule)
    {
    firewall_rule_found = true;
    Console.WriteLine("My Firewall Rule is in place!");
    }
}

但是,为了使用.NET核心进行进一步开发,我必须将项目迁移到.NET核心2.1或更高版本到.NET Core 2.2。

However, in order to use .NET core for further development, I have to migrate my project to .NET Core 2.1 or later to .NET Core 2.2.

但相同的代码只是停止工作。 我收到错误消息:

But the same code simply stop working.  I got error message:

System.IO.FileNotFoundException

  HResult = 0x80070002

 消息=无法加载文件或程序集'Interop.NetFwTypeLib,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。 

系统找不到该文件。

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not load file or assembly 'Interop.NetFwTypeLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. 
System can't find the file.

但是从Visual Studio 2017开始,在解决方案资源管理器中,我可以看到文件在那里,路径为:  obj \Debug \\\
etcoreapp2.1 \Interop.NetFwTypeLib.dll

But from Visual Studio 2017, in Solution Explorer, I can see the file is there, with the path: obj\Debug\netcoreapp2.1\Interop.NetFwTypeLib.dll

带有标识:  Interop.NetFwTypeLib

with identity: Interop.NetFwTypeLib

但版本信息为空。

请告知如何修复这个问题? 或者我可以在.NET Core中使用任何nuget包,我不喜欢仍然使用旧的Interop COM包。

Please advice how to fix this issue?  Or is there any nuget package I can use in .NET Core, I don't like to still use the old Interop COM package.

谢谢,

推荐答案

嗨zydjohn,

Hi zydjohn,

感谢您在此发帖。

根据您的描述,代码是否适用于.NET Core 2.1?如果是,您如何将项目从.NET Core 2.1迁移到.NET Core 2.2?也许迁移导致错误。 

According to your description, does the code works well in .NET Core 2.1? If yes, how do you migrate the project from .NET Core 2.1 to .NET Core 2.2? Maybe the migration cause the error. 

然后请检查dll。如果dll存在,也许dll会被破坏。

And then please check the dll. If the dll exists, maybe the dll is broken.

最好的问候,

Wendy


这篇关于如何在.NET核心2.1中使用NetFwTypeLib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆