Reputation: 53
I am developing a c# console application that requires some 3rd party assemblies(DLL). On dev machine, I know about the location of these assemblies so I can add them as reference to VS2010 project. But I am not sure of these assemblies path at client(user) machine. I can get these assemblies location from an environment variable or by an registry value ( may be added by 3rd party tool or by user). So how will my application load these assemblies?
Upvotes: 1
Views: 1381
Reputation: 62265
Well, if I rigth understoo your question is.
You have C#
application that staticly linked via AddReference to some DLL's in your Visual Studio Solution. But on client machine those DLLs are not in the same location, but, by the way kn some known location.
To resolve this, should be enough to use
before actually calling
Application.Run(new MainForm());
In this way, you will tell to your domain to lookup in custom look up path too for references.
Hope this helps.
Upvotes: 0
Reputation: 8357
If the assemblies are installed on the client, they are automatically loaded because they should be in the GAC. if not, they should be in the same folder as the .exe, or you can load them programmatically with the Appdomain.AssemblyResolve event. This event is fired if an assembly can't be found:
public MainWindow()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
InitializeComponent();
}
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string assemblyName = args.Name.Remove(args.Name.IndexOf(',')) + ".dll";
string s = @"C:\dlls\" + assemblyName;
if (File.Exists(s))
{
return Assembly.LoadFile(s);
}
return null;
}
//EDIT: Watch out with this, there are sometimes a lot of assemblies that can't be found, and it's usually not a problem because it just searches on other places. But if you decide to rewrite this and try to load all assemblies which aren't found, i.e. remove the file.exists, your code will break.
Upvotes: 2
Reputation: 49013
If you set the Copy Local
property of the referenced assembly to true
, your third party assemblies will be copied to the bin directory near your executable.
This means you'll have those dll in the same directory as your executable when you will install your application on another machine, so there won't be any issue to load those assemblies.
Upvotes: 1