Reputation: 153
Does anybody knows how to create relative reference in C# Visual Studio that will link the DLL from the path windows\assembly\GAC
?
My problem is that on my computer that DLL is located in GAC_MSIL\somePath\myDll.dll
, but on another computer it is in GAC\somePath\myDll.dll
.
Is it possible to write a reference (for example: %GAC%\somePath\myDll.dll
) which will find the path to my GAC folder and references it.
Upvotes: 0
Views: 2010
Reputation: 93
You just reference it and that's it. When you add a reference, it remembers the full assembly name, including assembly name, version and public key token (for strong named assembly). At runtime when your application tries to load that assembly, the loader will check the GAC first, if a match it found, it will load from GAC. If it can not find the assembly from GAC, it will go further (such as searching private bin folder, etc.) You can find more details here:
http://msdn.microsoft.com/en-us/library/yx7xezcf(v=vs.71).aspx
Upvotes: 2
Reputation: 8079
You just need to add the DLL to your project references. The program will automatically use the right DLL from the users GAC. This is exactly what the GAC (Global Application Cache) is for.
If the DLL is not found on the users machine you need to install it into the GAC first. Here is an example of how to do so (for an Excel DLL):
System.EnterpriseServices.Internal.Publish p = new System.EnterpriseServices.Internal.Publish();
FolderBrowserDialog fb = new FolderBrowserDialog();
fb.ShowDialog();
string pathToDll = fb.SelectedPath;
string excel = t + @"\" + "Microsoft.Office.Interop.Excel.dll";
if (!File.Exists(excel))
{
using (FileStream fs = new FileStream(excel, FileMode.CreateNew))
{
fs.Write(Properties.Resources.microsoft_office_interop_excel_11, 0, Properties.Resources.microsoft_office_interop_excel_11.Length);
fs.Close();
}
}
Console.WriteLine("Register GAC...");
p.GacInstall(excel);
The DLL is an application resource in this example and is written to disk first an then registered into the GAC.
Upvotes: 1