w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Add native dlls and other files to .net exe

The way I have done this is to store the DLL in a path within the project and use the SetDllDirectory method to tell my application where to look for the DLL.

Here is my example:

PInvoke Signature for SetDllDirectory()

 [DllImport("kernel32.dll", SetLastError = true)]
  static extern bool SetDllDirectory(string lpPathName);

Constructor for the class that contains the DllImport

 public ClassName()
     var currentWorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
     string path = Path.Combine(cwd, @"HereGoesYourRelativeFilepath");

On the properties of my DLL I have the "Copy To Output Directory" property set to "Copy Always" so I know that it will always be in the relevant folder in my bin directory after a build, so my relative filepath points to there and all is well.

Hope this helps

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.