.NET Core project with native library depedency
This should copy your folder structure to the output folder, just put it right away under the first </PropertyGroup>
in your MyApp.csproj
:
<ItemGroup>
<None Update="runtimes\**" CopyToOutputDirectory="PreserveNewest"/>
</ItemGroup>
Just in case, there is a method to have more control on how you are loading the native libraries using DllImport
by DllImportResolver
delegate for your assembly.
public delegate IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath);
There is also NativeLibrary
class which allows to set and load native libraries for .net core. Some sample code:
static class Sample
{
const string NativeLib = "NativeLib";
static Sample()
{
NativeLibrary.SetDllImportResolver(typeof(Sample).Assembly, ImportResolver);
}
private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
IntPtr libHandle = IntPtr.Zero;
//you can add here different loading logic
if (libraryName == NativeLib && RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.Is64BitOperatingSystem)
{
NativeLibrary.TryLoad("./runtimes/win-x64/native/somelib.dll", out libHandle);
} else
if (libraryName == NativeLib)
{
NativeLibrary.TryLoad("libsomelibrary.so", assembly, DllImportSearchPath.ApplicationDirectory, out libHandle);
}
return libHandle;
}
[DllImport(NativeLib)]
public static extern int DoSomework();
}