0

有谁知道如何在 C# Visual Studio 中创建将从路径链接 DLL 的相对引用windows\assembly\GAC
我的问题是,在我的计算机上,DLL 位于GAC_MSIL\somePath\myDll.dll,但在另一台计算机上GAC\somePath\myDll.dll
是否可以编写一个参考(例如%GAC%\somePath\myDll.dll:),它将找到我的 GAC 文件夹的路径并引用它。

4

2 回答 2

2

你只是参考它,就是这样。添加引用时,它会记住完整的程序集名称,包括程序集名称、版本和公钥标记(对于强命名程序集)。在运行时,当您的应用程序尝试加载该程序集时,加载程序将首先检查 GAC,如果找到匹配项,它将从 GAC 加载。如果它无法从 GAC 中找到程序集,它会进一步(例如搜索私人 bin 文件夹等)。您可以在此处找到更多详细信息:

http://msdn.microsoft.com/en-us/library/yx7xezcf(v=vs.71).aspx

于 2013-07-03T13:50:41.247 回答
1

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.

于 2013-07-03T13:36:30.180 回答