Support
Joined: 18 Jul 2005 Posts: 731
|
Posted: Fri Jun 05, 2009 5:59 am Post subject: HOWTO: Load dependant assemblies from your shell extension. |
|
|
If your shell extension has dependant assemblies, you may get a FileNotFoundException when you try to use the shell extension. This is becuase the .Net runtime tries to find the dependant assemblies in the path of the host exe (example: "c:\windows" for Windows Explorer, ) but does not find them there. The solution is to handle the AppDomain.AssemblyResolve event in the static contructor of the class representing your shell extension as follows:
[C#]
Code: | // MyShellExtension is the class representing the shell extension
static MyShellExtension()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.Contains("Dependant")) // .Net runtime is trying to load Dependant.dll assembly
{
// Load it from the same path as our shell extension dll and return it
string path = Assembly.GetExecutingAssembly().Location;
path = Path.GetDirectoryName(path);
path = Path.Combine(path, "Dependant.dll");
Assembly ret = Assembly.LoadFrom(path);
return ret;
}
return null;
} |
[VB.Net]
Code: | ' MyShellExtension is the class representing the shell extension
Shared Sub New()
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf CurrentDomain_AssemblyResolve
End Sub
Shared Function CurrentDomain_AssemblyResolve(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly
If (args.Name.Contains("Dependant")) Then ' .Net runtime is trying to load Dependant.dll assembly
'Load it from the same path as our nse dll and return it
Dim path As String = Assembly.GetExecutingAssembly().Location
path = System.IO.Path.GetDirectoryName(path)
path = System.IO.Path.Combine(path, "Dependant.dll")
Dim ret As Assembly = Assembly.LoadFrom(path)
Return ret
End If
Return Nothing
End Function |
|
|