Share via


How to use NetFwTypeLib in .NET core 2.1

Question

Monday, September 10, 2018 7:30 PM

Hello:

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".

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!");
    }
}

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
  Message=Could not load file or assembly 'Interop.NetFwTypeLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. 
System can't find the file.

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

with identity: Interop.NetFwTypeLib

But the version information is empty.

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.

Thanks,

All replies (4)

Monday, September 10, 2018 7:42 PM | 1 vote

You can use P/Invoke.

I had posted a sample in this thread


Tuesday, September 11, 2018 7:26 AM

Hi zydjohn,

Thank you for posting here.

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. 

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

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Thursday, September 13, 2018 7:41 AM

Hello:

I didn't say my project is working in .NET Core 2.1, it stops working.  But my code worked in .NET Framework 4.7.2 with C# 7.3.  And I don't like to use the legacy COM package in .NET Core 2.1.

Is there any better solution?  The issue is: for a number of people who have projects in .NET Framework, if they want to migrate to .NET Core, but if many things are not working any more, how they can proceed?

Thanks,

John 


Thursday, September 20, 2018 2:15 AM | 1 vote

Hi zydjohn,

There are many differences for libraries between .net framework and .net core. If you migrate from .net framework to .net core, it is possible that what library you're using that isn't available on .NET Core.

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].