Search code examples
c#outlookadd-indllimport

How can I find the location of DLLs included as resources in an outlook add-in for use with DllImport


I have an outlook add-in in which I need to load a custom dll using DllImport. I have included the dll in the project resources and when the project is 'published' it is copied to the Resources folder.

My problem is that after a user installs the add-in I do not know where the DLLs I have included in the resources are. If I knew then I could add that location to the current search path and everything would work.

Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";" + dllDirectory);

Does anybody have a solution or am I going about this all wrong?


Solution

  • //use CodeBase instead of Location because of Shadow Copy.
    string codebase = Assembly.GetExecutingAssembly().CodeBase;
    var vUri = new UriBuilder(codebase);
    string vPath = Uri.UnescapeDataString(vUri.Path + vUri.Fragment);
    string directory = Path.GetDirectoryName(vPath);
    if (!string.IsNullOrEmpty(vUri.Host)) directory = @"\\" + vUri.Host + directory;
    DllLocation = Path.Combine(directory, "Resources\\MyDll.dll");