1

我已经搜索了论坛,并且存在很多关于上述问题的答案。但是,在我的情况下,我从软件插件中引用了一个 dll。似乎这使情况变得更加复杂,而且我找到的解决方案都没有返回正确的位置。

我尝试了以下方法:

Print(AppDomain.CurrentDomain.BaseDirectory); // returns C:\Program Files\TheOtherSoftware\System\
System.Type MyDLLsNameType = null;
Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
foreach (AssemblyName an in a.GetReferencedAssemblies() )
{
  Print(an.Name); // finds various names including MyDLLsName
  Print(an.CodeBase);  // this is null
  if (an.Name == "MyDLLsName"){
    MyDLLsNameType = an.GetType();
  }
}
Print(System.Reflection.Assembly.GetAssembly(MyDLLsNameType).Location); // this returns  "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll"

作为旁注:我想这样做是因为我想摆脱需要用户在特定位置安装软件的各种绝对路径。当根驱动器未命名为“C”时,该软件甚至崩溃了。鉴于我在编码方面没有太多经验,因此很高兴听到一些有关如何处理此类问题的建议。

4

0 回答 0