安装到 GAC 的 DLL 路径

发布于 2024-07-11 07:26:11 字数 503 浏览 4 评论 0原文

如何获取(可能)在 GAC 中注册的 DLL 的(物理)安装路径? 此 DLL 是一个控件,可能托管在 .Net 应用程序以外的其他事物中(包括 VS 以外的 IDE...)。

当我使用 System.Reflection.Assembly.GetExecutingAssembly().Location 时,它给出了 winnt\system32 中 GAC 文件夹的路径 - 或者在 VS 的设计模式下给出 VS IDE 的路径。

我需要获取实际安装物理 dll 的路径 - 或者 VS 的 bin/debug 或(发布)文件夹。

原因是我需要在此文件夹中获取一个 XML 文件,其中包含在设计模式和运行时使用的配置设置。

或者如何最好地处理这种情况? 我目前正在使用一个可疑的网络位置进行设计模式...(不要认为 ApplicationData 文件夹会删除它(但已保存 .Net 版本,因为它是通过安装的) ClickOnce 可以使用 Clickonce Data 文件夹))

How can I get the (physical) installed path of a DLL that is (may be) registered in GAC? This DLL is a control that may be hosted in things other than a .Net app (including IDEs other than VS...).

When I use System.Reflection.Assembly.GetExecutingAssembly().Location, it gives path of GAC folder in winnt\system32 - or in Design mode in VS gives the path to the VS IDE.

I need to get the path where physical dll is actually installed - or the bin/debug or (release) folder for VS.

Reason is that there is an XML file I need to get at in this folder, with config setting that are used both in design mode and at runtime.

Or how is it best to handle this scenario? I have a dubious network location I am using for design mode at the moment... (Don't think that ApplicationData folder is going to cut it (but have the .Net version soved as that's installed via ClickOnce ans can use the Clickonce Data folder) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果将某些内容放入 GAC 中,它实际上会被复制到 %WINDIR%\ assembly 下的某个位置,就像

C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll

我假设您在检查相关程序集安装在 GAC 中的位置时会看到类似的内容。 这实际上是正确的。 (在 .NET 1.1 中,当您查看 GAC 程序集的属性时,会列出一个“代码库”,但这只是为了向您显示运行 gacutil 时原始文件所在的位置 - 它实际上并不指示将加载什么内容。 )您可以在此处阅读更多相关信息

长话短说,你可能无法做你想做的事。 您可能希望将行为切换为相对于主应用程序程序集 (Assembly.GetEntryAssembly()) 进行查找,而不是查看正在加载的某些程序集 (Assembly.GetExecutingAssembly()))或将文件放在某个众所周知的位置(可能基于设置的环境变量)。

If something gets put in the GAC, it actually gets copied into a spot under %WINDIR%\assembly, like

C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll

I assume you're seeing something like that when you check the Location of the assembly in question when it's installed in the GAC. That's actually correct. (In .NET 1.1 there was a "Codebase" listed when you looked at a GAC assembly's properties, but that was only to show you where the original file was located when you ran gacutil - it didn't actually indicate what would be loaded.) You can read more about that here.

Long story short, you may not be able to do what you want to do. Instead of looking in relation to some assembly that's being loaded (Assembly.GetExecutingAssembly()), you might want to switch the behavior to look relative to the primary application assembly (Assembly.GetEntryAssembly()) or put the file in some well-known location, possibly based on an environment variable that gets set.

国产ˉ祖宗 2024-07-18 07:26:11

将程序集影子复制到全局程序集缓存后,我认为没有任何元数据可以追溯源程序集的位置。

您想通过在 GAC 中部署来实现什么目标? 如果只是为了 CLR 来解决问题,那么还有另一种方法可以解决您的问题。

不要 gac 安装 dll,而是在注册表中添加以下键(当尝试解析程序集时,CLR 会查找此注册表位置)

32 bit OS : HKEY_LOCAL_MACHINE\SOFTWARE\\Microsoft\.NETFramework\v4.0.30319\AssemblyFoldersEx\foo

64 bit OS : HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319\AssemblyFoldersEx\foo

对于 foo 键(使用您最喜欢的名称而不是 foo),您将看到键名“默认”。 双击它并将值设置为程序集所在的位置。 (首选绝对路径)

现在,从 Visual Studio 中,您的客户端应该能够在“添加引用”对话框中看到您的程序集并可以使用它。

现在回到您的实际问题,

Assembly.GetExecutingAssembly() 将返回已安装 dll 所在位置的路径。 从那里找到 XML 文件。 :)

注意:在注册表项中,4.0.30319 是您的应用程序所针对的 .NET Framework 的版本。 请使用您的应用程序的目标版本。

After the assembly is shadow copied into the Global Assembly cache, i don't think there is any metadata to traceback the location of the source assemblies.

What are you trying to achieve by deploying in GAC? If its just for the sake of CLR for resolving purposes, then there is an alternate way that solves your problem.

Don't gac install the dll, rather add the following key in the registry, (this registry location is looked up by CLR when trying to resolve assemblies)

32 bit OS : HKEY_LOCAL_MACHINE\SOFTWARE\\Microsoft\.NETFramework\v4.0.30319\AssemblyFoldersEx\foo

64 bit OS : HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319\AssemblyFoldersEx\foo

For the foo key (Use your favourite name instead of foo), you will see a Key Name "Default". Double click it and set the value to wherever your assembly exists. (absolute path is preferred)

Now from Visual Studio, your client should be able to see your assemblies in the "Add Reference" Dialog and can use it.

Now coming to your actual problem,

Assembly.GetExecutingAssembly() will return the path of the location where the insatlled dll's are present. Find the XML file from there. :)

Note: In the registry key the 4.0.30319 is the version of the .NET Framework your application targets. Use whatever version your application targets instead.

征棹 2024-07-18 07:26:11

您可以选择将资源嵌入到该 DLL 中吗? 这样,DLL 位于磁盘上的哪个位置并不重要,因为 XML 文件将跟随它。 然后你可以做这样的事情:

Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyXmlFile.xml");
XmlDocument d = new XmlDocument();
using (StreamReader r = new StreamReader(s))
{
    d.LoadXml(r.ReadToEnd());
}

Do you have the option of embedding a resource to this DLL? That way, it doesn't really matter where the DLL is located on disk, because the XML file will follow it. You can then do something like this:

Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProject.MyXmlFile.xml");
XmlDocument d = new XmlDocument();
using (StreamReader r = new StreamReader(s))
{
    d.LoadXml(r.ReadToEnd());
}
别靠近我心 2024-07-18 07:26:11

如果您正在查找文件系统中保存 GACed DLL 的物理位置,请尝试以下操作:
开始-->运行-->c:\windows\ assembly\gac
如果您在其中找不到 DLL 相关文件夹,您可以在 Windows 资源管理器中执行“向上”文件夹操作,将 c:\windows\ assembly 中的所有内容显示为文件夹结构。 然后,您可以在 GAC_MSIL 或任何其他文件夹下查找 DLL...

干杯,
斯里

If you are looking for the physical location where your GACed DLL is saved in the file system, try this:
start-->run-->c:\windows\assembly\gac
If you don't find your DLL related folder in there, you can do a "Up" folder in windows explorer to display everything in c:\windows\assembly as folder structures. You can then look for your DLL under GAC_MSIL or any other folder out there....

Cheers,
Sri

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文