Landin Martens
Landin Martens

Reputation: 3333

C# load DLL from different folder?

I have a C# project that contains 1 EXE and about 7 DLLs. What I would like to have is a folder beside the EXE called "Library" (or something similar) that contains all the DLLs so that it is a bit more organized and looks better for the end user.

I know this can be done using an AppConfig but the I don't want another file beside the EXE. All I want is the main EXE and the folder.

Is it possible to use AppConfig and embed it or load the DLLs without using a AppConfig that won't change how I currently use my DLLs? I know you can load a DLL at run time but I don't think that is what I am looking for.

Thanks!

EDIT

I know the pros and cons to doing this, so please only answers on how to do this and no advice as to why I should or should not do this.

Upvotes: 8

Views: 13415

Answers (4)

gevaraweb
gevaraweb

Reputation: 923

I created new console application-launcher. In app folder contains my EXE file - MyApp.exe and all DLLs.

static void Main(string[] args)
    {
        var process = new Process
            {
                StartInfo =
                     {
                       FileName = "app\\MyApp.exe",
                     }
            };
        process.Start();
    }

If you want the application to be launched when you drag certain files onto the EXE:

class Program
{
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            String FileName = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location )
                                        + "\\app\\MyApp.exe";
            ProcessStartInfo startInfo = new ProcessStartInfo(FileName);
            startInfo.WindowStyle = ProcessWindowStyle.Normal;
            startInfo.Arguments = args[0].ToString();
            Process.Start(startInfo);
        }

        else { 
            var process = new Process
                {
                    StartInfo =
                              {
                                  FileName = "app\\MyApp.exe",
                              }
                };
            process.Start();
        }
    }
}

Upvotes: 0

zeromus
zeromus

Reputation: 1667

SetDllDirectory() + carefully coded AssemblyResolve. Works for me in a nontrivial project, with no DLL hell.

https://github.com/TASVideos/BizHawk/blob/d05ddabf5f22debf47369f94868462a75ea0b466/BizHawk.Client.EmuHawk/Program.cs

Upvotes: 0

avijendr
avijendr

Reputation: 4153

PrettyBin is your solution. It does this beautifully!

Upvotes: 1

doug65536
doug65536

Reputation: 6771

Use System.Reflection.Assembly.LoadFrom(path).

LoadFrom will allow it to look in the same folder as the targetted dll for any dependencies. If you use Load, then it will not consider dlls that are sitting in the same folder as the dll you Load.

I know this doesn't directly answer your question, but manually calling LoadFrom on the DLLs early in your process startup should do the trick if you want an "xcopy" installable .net app or something.

Upvotes: 3

Related Questions