Assembly.Location 给出了程序集的简单路径。不幸的是,在单元测试或 ASP.NET 等阴影环境中运行时,这是空的。但是,Codebase 属性可用,并提供了一个可以替代使用的 URI。在哪些情况下它不返回以 开头的 URI file:///
?或者换句话说:在哪些情况下这不起作用或会返回不可用的结果?
Assembly assembly = GetType().Assembly;
Uri codeBaseUri = new Uri(assembly.CodeBase);
string path = codeBaseUri.LocalPath;