views:

69

answers:

4

Is there a way in .NET C# Console Application to deploy the executable to a different directory than the DLL's it depends on?

In this case I would like to structure my deployment so that on the server where this will run I have the following directory structure. c:\app\bin\sample.exe c:\app\dll*.dll

A: 

You can change the output path of your DLLs so that they are put into a path other than the default one. This is done in the Build page of the Project Designer.

See the following article: http://msdn.microsoft.com/en-us/library/kb4wyys2%28VS.80%29.aspx

Bernard
+1  A: 

You can always use the AssemblyResolveEvent to specify location of any assembly like so:

public static void Main(string[] args)
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

    //do something
}

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    Assembly.LoadFrom(fileName); // Load assembly from any file
    return assembly;
}

That method gets called every time it can't find the location of your file. Another option is to specify a directory in the 'Reference path' section of your program's properties page.

Pierre-Olivier Goulet
This won't work as posted. Perhaps you can make it helpful by showing the OP how to use Assembly.GetEntryAssembly().Location to generate the proper path.
Hans Passant
Well no it won't work as posted, because fileName is not declared... All you have to do is define fileName as the location of the DLL. You could use Assembly.GetExecutingAssembly() to get the location of the EXE, than find the relative location of the DLL
Pierre-Olivier Goulet
By the way, I agree with Hans post about this not being the best idea... I did something similar for one of my projects, and it creates a bunch of problems, including security problems, witch are a real pain in the ...
Pierre-Olivier Goulet
A: 

One solution could be deploying your DLLs to the GAC, Global Assembly Cache. This way your application will load them from the GAC instead of loading them from the application directory.

sh_kamalh
+2  A: 

It is fairly unwise, the CLR cannot find the DLL without help. Your customer won't care much about the location of the DLL. In fact, I think most IT staff prefer binaries in the same directory.

If you put the DLL in a c:\app\bin\dll subdirectory then you can use an app.exe.config file with the <probing> element to tell the CLR to look in that directory. Deploying to c:\app\dll is much harder, it requires a very unpractical <codeBase> in a <dependentAssembly>. Which makes the app unmovable, prefer Pierre's solution instead. Except that it needs work, you want to use Assembly.GetEntryAssembly().Location to get the install path of the EXE do that you can generate a relative path off that.

Hans Passant
I wasn't aware of the ability to defing a location for the app to search for DLL's via <probing>. Given the potential issues caused by moving the assemblies outside of the application directory this sounds like a better solution that. Thanks for your help.
nardbard