I want to lo开发者_JAVA技巧ad my assemblies from a location other than the bin folder.
How can I accomplish this ?
Thank you
note:
My previous answer was incorrect. Sam Holder's supposition sounded plausible so I wrote some tests to confirm.
Consider it confirmed.
To test, add a reference to a project or dll and set 'Copy Local' to false. Make sure you 'show all files' and delete the bin dir or you are going to have a lingering dll in there.
Global.asax
namespace WebApplication1
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);
}
System.Reflection.Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name == "ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
{
String assemblyPath = @"C:\Projects\StackOverflowAnswers\WebApplication1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
Assembly assembly = Assembly.LoadFrom(assemblyPath);
return assembly;
}
return null;
}
.....
The OP should perhaps give me an upvote but Sam should get the check.
I'm not sure if you can do this in asp.net, but in .net applications you can provide a delegate to the AssemblyResolve event in the current AppDomain
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += assemblyResolver.ResolveEventHandler;
when the application has any assembly references that it can't resolve it will call this delegate to get the assembly resolved. You can then simply return the assembly requested from the delegate:
String assemblyPath = //some logic to determine the location of the assembly
Assembly assembly = Assembly.LoadFrom (assemblyPath);
return assembly;
Add the folder you do want to use to your PATH environment variable. This will let you type my-program.exe from the command line and it will run.
If you are trying to load something internally as part of your program, you can change the working directory SetCurrentDirectory
or just use the full path for each file access.
精彩评论