自动jquery弹出一个窗口口加载hellp dll失败。?加载 - 游戏 -…

How to add folder to assembly search path at runtime in .NET? - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.1 million programmers, just like you, helping each other.
J it only takes a minute:
My DLLs are loaded by a third-party application, which we can not customize. My assemblies have to be located in their own folder. I can not put them into GAC (my application has a requirement to be deployed using XCOPY).
When the root DLL tries to load resource or type from another DLL (in the same folder), the loading fails (FileNotFound).
Is it possible to add the folder where my DLLs are located to the assembly search path programmatically (from the root DLL)? I am not allowed to change the configuration files of the application.
39.9k1166122
1,28161727
Sounds like you could use the AppDomain.AssemblyResolve event and manually load the dependencies from your DLL directory.
Edit (from the comment):
AppDomain currentDomain = AppDomain.CurrentD
currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);
static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string assemblyPath = bine(folderPath, new AssemblyName(args.Name).Name + ".dll");
if (!File.Exists(assemblyPath))
Assembly assembly = Assembly.LoadFrom(assemblyPath);
2,38311428
You can add a
to your application's .config file, but it will only work if the probing path is a contained within your application's base directory.
159k25281463
Update for Framework 4
Since Framework 4 raise the AssemblyResolve event also for resources actually this handler works better. It's based on the concept that localizations are in app subdirectories (one for localization with the name of the culture i.e. C:\MyApp\it for Italian)
Inside there are resources file.
The handler works also if the localization is country-region i.e. it-IT or pt-BR. In this case the handler "might be called multiple times: once for each culture in the fallback chain" [from MSDN]. This means that if we return null for "it-IT" resource file the framework raises the event asking for "it".
Event hook
AppDomain currentDomain = AppDomain.CurrentD
currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve);
Event handler
Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
//This handler is called only when the common language runtime tries to bind to the assembly and fails.
Assembly executingAssembly = Assembly.GetExecutingAssembly();
string applicationDirectory = Path.GetDirectoryName(executingAssembly.Location);
string[] fields = args.Name.Split(',');
string assemblyName = fields[0];
string assemblyC
if (fields.Length & 2)
assemblyCulture =
assemblyCulture = fields[2].Substring(fields[2].IndexOf('=') + 1);
string assemblyFileName = assemblyName + ".dll";
string assemblyP
if (assemblyName.EndsWith(".resources"))
// Specific resources are located in app subdirectories
string resourceDirectory = bine(applicationDirectory, assemblyCulture);
assemblyPath = bine(resourceDirectory, assemblyFileName);
assemblyPath = bine(applicationDirectory, assemblyFileName);
if (File.Exists(assemblyPath))
//Load the assembly from the specified path.
Assembly loadingAssembly = Assembly.LoadFrom(assemblyPath);
//Return the loaded assembly.
return loadingA
6,86711729
The best explanation :
AppDomain currentDomain = AppDomain.CurrentD
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
//This handler is called only when the common language runtime tries to bind to the assembly and fails.
//Retrieve the list of referenced assemblies in an array of AssemblyName.
Assembly MyAssembly, objExecutingA
string strTempAssmbPath = "";
objExecutingAssembly = Assembly.GetExecutingAssembly();
AssemblyName[] arrReferencedAssmbNames = objExecutingAssembly.GetReferencedAssemblies();
//Loop through the array of referenced assembly names.
foreach(AssemblyName strAssmbName in arrReferencedAssmbNames)
//Check for the assembly names that have raised the "AssemblyResolve" event.
if(strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
//Build the path of the assembly from where it has to be loaded.
strTempAssmbPath = "C:\\Myassemblies\\" + args.Name.Substring(0,args.Name.IndexOf(","))+".dll";
//Load the assembly from the specified path.
MyAssembly = Assembly.LoadFrom(strTempAssmbPath);
//Return the loaded assembly.
return MyA
32.6k30200248
For C++/CLI users, here is @Mattias S' answer (which works for me):
using namespace S
using namespace System::IO;
using namespace System::R
static Assembly ^LoadFromSameFolder(Object ^sender, ResolveEventArgs ^args)
String ^folderPath = Path::GetDirectoryName(Assembly::GetExecutingAssembly()-&Location);
String ^assemblyPath = Path::Combine(folderPath, (gcnew AssemblyName(args-&Name))-&Name + ".dll");
if (File::Exists(assemblyPath) == false)
Assembly ^assembly = Assembly::LoadFrom(assemblyPath);
// put this somewhere you know it will run (early, when the DLL gets loaded)
System::AppDomain ^currentDomain = AppDomain::CurrentD
currentDomain-&AssemblyResolve += gcnew ResolveEventHandler(LoadFromSameFolder);
look into AppDomain.AppendPrivatePath (deprecated) or AppDomainSetup.PrivateBinPath
protected by
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10
on this site (the ).
Would you like to answer one of these
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
rev .25896
Stack Overflow works best with JavaScript enabled

参考资料

 

随机推荐