类库的C#获取路径 [英] C# Get path of class library

查看:557
本文介绍了类库的C#获取路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用一些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

这篇关于类库的C#获取路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆