Assembly.CodeBase:什么时候没有文件-URI?

发布于 2024-08-08 15:11:04 字数 333 浏览 3 评论 0 原文

Assembly.Location 给出了程序集的简单路径。不幸的是,当在影子环境(例如单元测试或 ASP.NET)中运行时,该值是空的。不过,Codebase 属性可用,并提供了可以替代使用的 URI。在什么情况下它不返回以 file:/// 开头的 URI?或者换句话说:在什么情况下这不起作用或将返回不可用的结果?

Assembly assembly = GetType().Assembly;    
Uri codeBaseUri = new Uri(assembly.CodeBase);
string path = codeBaseUri.LocalPath;

Assembly.Location gives a plain path to the assembly. Unfortunately this is empty when running in a shadowed environment, such as unit test or ASP.NET. Hovever, the Codebase property is available and provides a URI that can be used instead. In which cases it returns no URI starting with file:///? Or in other words: what are the cases in which this won't work or will return unusable results?

Assembly assembly = GetType().Assembly;    
Uri codeBaseUri = new Uri(assembly.CodeBase);
string path = codeBaseUri.LocalPath;

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

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

发布评论

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

评论(3

楠木可依 2024-08-15 15:11:04

可以直接通过 HTTP 加载程序集,例如在 ClickOnce 部署中:

Assembly assembly = Assembly.LoadFrom("http://server/app.dll");
Uri codeBaseUri = new Uri(assembly.CodeBase);
Debug.Assert(codeBaseUri.Scheme == "http"); 
Debug.Assert(codeBaseUri.LocalPath == "");
Debug.Assert(assembly.Location == "");

It's possible to load assemblies directly over HTTP, such as in a ClickOnce deployment:

Assembly assembly = Assembly.LoadFrom("http://server/app.dll");
Uri codeBaseUri = new Uri(assembly.CodeBase);
Debug.Assert(codeBaseUri.Scheme == "http"); 
Debug.Assert(codeBaseUri.LocalPath == "");
Debug.Assert(assembly.Location == "");
冰雪梦之恋 2024-08-15 15:11:04

此外,#char 作为片段分隔符也受到威胁。
请参阅 http://blogs.msdn.com /ncl/archive/2010/02/23/system-uri-faq.aspx(第 9 节)进行简短说明。

使用 Assembly.EscapedCodeBase 而不是 Assembly.CodeBase 来对抗它。

Also, # char is threated as a fragment separator.
See http://blogs.msdn.com/ncl/archive/2010/02/23/system-uri-f-a-q.aspx (section 9) for short explanation.

Use Assembly.EscapedCodeBase instead of Assembly.CodeBase to fight it.

不知所踪 2024-08-15 15:11:04

Ancient question, but I found a relevant, consderably new feature changing the picture: packing a dotnet core app into a single file will break Assembly.CodeBase according to https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file#api-incompatibility

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