类库的C#获取路径路径、类库

2023-09-06 09:17:29 作者:枫叶羽

我有一个使用一些XML文件的类库在自己的目录中找到。

I have a class library that uses some xml files found in its own directory.

当从其他项目引用这个库,我怎么确保库是从它自己的目录中工作?

When referencing this library from another project, how do I ensure the library is working from it's own directory?

我试过Assembly.GetExecutingAssembly()的位置,但仍返回启动项目的路径。

I tried Assembly.GetExecutingAssembly().Location but that still returns the path of the startup project.

推荐答案

我知道这是旧的文章,但在事件别人绊倒它从搜索,我发现这样做最简单的方法是:

I know this is an old post but in the event someone else stumbles on it from a search, the easiest way I've found to do this is:

Public Class Foo

     Public Shared Function GetMyPath() As String
          Return Path.GetDirectoryName(GetType(Foo).Assembly.Location)
     End Function

End Class