Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

c# - 如何获取代码所在的程序集的路径?(How do I get the path of the assembly the code is in?)

Is there a way to get the path for the assembly in which the current code resides?

(有没有办法获取当前代码所在的程序集的路径?)

I do not want the path of the calling assembly, just the one containing the code.

(我不希望调用程序集的路径,而只是包含代码的路径。)

Basically my unit test needs to read some xml test files which are located relative to the dll.

(基本上,我的单??元测试需要读取一些相对于dll的xml测试文件。)

I want the path to always resolve correctly regardless of whether the testing dll is run from TestDriven.NET, the MbUnit GUI or something else.

(我希望该路径始终正确解析,而不管测试dll是从TestDriven.NET,MbUnit GUI还是其他版本运行。)

Edit : People seem to be misunderstanding what I'm asking.

(编辑 :人们似乎误解了我在问什么。)

My test library is located in say

(我的测试库位于)

C:\projects\myapplication\daotests\bin\Debug\daotests.dll

(C:\ projects \ myapplication \ daotests \ bin \ Debug \ daotests.dll)

and I would like to get this path:

(我想走这条路:)

C:\projects\myapplication\daotests\bin\Debug\

(C:\ projects \ myapplication \ daotests \ bin \ Debug \)

The three suggestions so far fail me when I run from the MbUnit Gui:

(当我从MbUnit Gui运行时,到目前为止,这三个建议使我失望:)

  • Environment.CurrentDirectory gives c:\Program Files\MbUnit

    (Environment.CurrentDirectory给出c:\ Program Files \ MbUnit)

  • System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location gives C:\Documents and Settings\george\Local Settings\Temp\ ....\DaoTests.dll

    (System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location给出C:\ Documents and Settings \ george \ Local Settings \ Temp \ .... \ DaoTests.dll)

  • System.Reflection.Assembly.GetExecutingAssembly().Location gives the same as the previous.

    (System.Reflection.Assembly.GetExecutingAssembly().Location与上一个相同。)

  ask by George Mauer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've defined the following property as we use this often in unit testing.

(我定义了以下属性,因为我们在单元测试中经常使用此属性。)

public static string AssemblyDirectory
{
    get
    {
        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        return Path.GetDirectoryName(path);
    }
}

The Assembly.Location property sometimes gives you some funny results when using NUnit (where assemblies run from a temporary folder), so I prefer to use CodeBase which gives you the path in URI format, then UriBuild.UnescapeDataString removes the File:// at the beginning, and GetDirectoryName changes it to the normal windows format.

(在使用NUnit时, Assembly.Location属性有时会给您一些有趣的结果(其中程序集从一个临时文件夹运行),所以我更喜欢使用CodeBase来为您提供URI格式的路径,然后UriBuild.UnescapeDataString删除File://开头, GetDirectoryName将其更改为正常的Windows格式。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...