C# 安装到 GAC 的 DLL 路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/377551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Path of DLL installed to the GAC
提问by kpollock
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...).
如何获取(可能)在 GAC 中注册的 DLL 的(物理)安装路径?此 DLL 是一个控件,它可以托管在 .Net 应用程序以外的东西中(包括 VS 以外的 IDE...)。
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.
当我使用 System.Reflection.Assembly.GetExecutingAssembly().Location 时,它给出了 winnt\system32 中 GAC 文件夹的路径 - 或者在 VS 中的设计模式下给出了 VS IDE 的路径。
I need to get the path where physical dll is actually installed - or the bin/debug or (release) folder for VS.
我需要获取实际安装物理 dll 的路径 - 或 VS 的 bin/debug 或(发布)文件夹。
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.
原因是我需要在此文件夹中获取一个 XML 文件,其中包含在设计模式和运行时都使用的配置设置。
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) )
或者如何最好地处理这种情况?我有一个可疑的网络位置,我目前用于设计模式......(不要认为 ApplicationData 文件夹会削减它(但是通过 ClickOnce 安装的 .Net 版本已解决,并且可以使用 Clickonce Data文件夹) )
采纳答案by Travis Illig
If something gets put in the GAC, it actually gets copied into a spot under %WINDIR%\assembly, like
如果将某些内容放入 GAC,它实际上会被复制到 %WINDIR%\assembly 下的某个位置,例如
C:\WINDOWS\assembly\GAC_32\System.Data.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.
我假设当您在 GAC 中安装有问题的程序集时检查它的位置时,您会看到类似的内容。这其实是正确的。(在 .NET 1.1 中,当您查看 GAC 程序集的属性时,会列出一个“代码库”,但这只是为了向您显示运行 gacutil 时原始文件的位置——它实际上并未指示将加载什么。 ) 您可以在此处阅读更多相关信息。
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.
长话短说,你可能无法做你想做的事。而不是寻找就该真实加载某些组件(Assembly.GetExecutingAssembly()
),您可能需要切换的行为相对于组装看主要的应用程序(Assembly.GetEntryAssembly()
),或者把文件中的某些众所周知的位置,可能是基于一个环境变量,获得放。
回答by BFree
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:
您是否可以选择将资源嵌入到此 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());
}
回答by Travis Illig
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....
如果您正在寻找 GACed DLL 在文件系统中保存的物理位置,请尝试以下操作:开始-->运行-->c:\windows\assembly\gac 如果在那里找不到与 DLL 相关的文件夹,您可以在 Windows 资源管理器中创建一个“向上”文件夹,以将 c:\windows\assembly 中的所有内容显示为文件夹结构。然后,您可以在 GAC_MSIL 或任何其他文件夹下查找您的 DLL....
Cheers, Sri
干杯,斯里
回答by Soundararajan
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.
你想通过在 GAC 中部署来实现什么?如果只是为了 CLR 的解决目的,那么有一种替代方法可以解决您的问题。
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)
不要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
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)
对于 foo 键(使用您喜欢的名称而不是 foo),您将看到一个键名“默认”。双击它并将值设置为您的程序集所在的位置。(绝对路径是首选)
Now from Visual Studio, your client should be able to see your assemblies in the "Add Reference" Dialog and can use it.
现在从 Visual Studio 中,您的客户端应该能够在“添加引用”对话框中看到您的程序集并可以使用它。
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. :)
Assembly.GetExecutingAssembly()
将返回安装的 dll 所在位置的路径。从那里找到 XML 文件。:)
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.
注意:在注册表项中,4.0.30319 是您的应用程序目标的 .NET Framework 版本。改用您的应用程序目标的任何版本。