简体   繁体   English

C#:在GAC中找到PresentationCore.dll(和其他程序集)的路径

[英]C#: finding path to PresentationCore.dll (and other assemblies) in the GAC

I need to reference various assemblies in a CSharpCodeProvider via calling ReferencedAssemblies.Add . 我需要通过调用ReferencedAssemblies.Add来引用CSharpCodeProvider各种程序集。

For some assemblies, it is sufficient to simply pass the assembly name - for example ReferencedAssemblies.Add("System.dll") . 对于某些程序集,只需传递程序集名称就足够了-例如ReferencedAssemblies.Add("System.dll")

However, PresentationCore.dll is one of those where a full path to the assembly is required - which I may not necessarily know on the target machine. 但是, PresentationCore.dll是其中需要程序集完整路径的程序之一-我可能不一定在目标计算机上知道。

Question: 题:

How can I locate the definitive path to the (latest?) respective assembly - say, in the GAC? 如何找到(最新?)各个程序集的确定路径-例如,在GAC中?

Ideally, the solution would not only work for PresentationCore.dll , but also other assemblies such as PresentationFramework.dll , System.Xaml.dll or System.Windows.Forms . 理想情况下,该解决方案不仅适用于PresentationCore.dll ,而且适用于其他程序集,例如PresentationFramework.dllSystem.Xaml.dllSystem.Windows.Forms

Sources that didn't yield the intended results: 未产生预期结果的来源:

  • This solution would involve hard-coding a specific path, but the solution should be dynamic 该解决方案将涉及对特定路径进行硬编码,但是该解决方案应该是动态的
  • Implementing this Windows API may work; 实施此Windows API可能有效; but I don't know how - or if it would even help solve the above 但我不知道如何-甚至是否可以解决上述问题
  • This C++ question refers to where the GAC is found, but not how to get an actual file path 这个 C ++问题是指在哪里找到GAC,而不是如何获取实际文件路径

You can reference the assemblies in the code that should do the resolving. 您可以在应该执行解析的代码中引用程序集。 Then the runtime will tell you the assembly location: 然后,运行时将告诉您程序集的位置:

typeof([TypeKnownToBeInTheDesiredAssembly]).Assembly.CodeBase

for example, to find the System.Xml.dll: 例如,找到System.Xml.dll:

string codeBase = typeof(System.Xml.XmlDocument).Assembly.CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
Console.WriteLine(path);

[ref] [参考文献]

On my system: 在我的系统上:

C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM