是否有方法获取当前代码所在程序集的路径?我不需要调用程序集的路径,只需要包含代码的路径。

基本上,我的单元测试需要读取一些相对于dll的xml测试文件。无论测试dll是否从TestDriven运行,我都希望该路径始终能够正确解析。NET, MbUnit GUI或者别的什么。

编辑:人们似乎误解了我的问题。

我的测试库位于say

C: \ \项目myapplication \ daotests \ bin \ \ daotests.dll调试

我想得到这条路径:

C: \ \ myapplication \ daotests \ bin \项目调试\

到目前为止,当我从MbUnit Gui运行时,这三个建议都失败了:

环境。CurrentDirectory 给出c:\Program Files\MbUnit System.Reflection.Assembly.GetAssembly .Location (typeof (DaoTests)) 给出C:\Documents和 乔治\ \本地设置 设置\ Temp \…\ DaoTests.dll .Location System.Reflection.Assembly.GetExecutingAssembly () 给出与前面相同的结果。


当前回答

这是我想到的。在web项目之间,单元测试(nunit和resharper测试运行器);我发现这对我很有用。

我一直在寻找代码来检测构建在什么配置中,调试/发布/定制名。唉,#if DEBUG. #如果有人能改进的话!

请随意编辑和改进。

获取应用程序文件夹。有用的web根,单元测试,以获得测试文件的文件夹。

public static string AppPath
{
    get
    {
        DirectoryInfo appPath = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);

        while (appPath.FullName.Contains(@"\bin\", StringComparison.CurrentCultureIgnoreCase)
                || appPath.FullName.EndsWith(@"\bin", StringComparison.CurrentCultureIgnoreCase))
        {
            appPath = appPath.Parent;
        }
        return appPath.FullName;
    }
}

获取bin文件夹:用于使用反射执行程序集。如果文件复制到那里由于构建属性。

public static string BinPath
{
    get
    {
        string binPath = AppDomain.CurrentDomain.BaseDirectory;

        if (!binPath.Contains(@"\bin\", StringComparison.CurrentCultureIgnoreCase)
            && !binPath.EndsWith(@"\bin", StringComparison.CurrentCultureIgnoreCase))
        {
            binPath = Path.Combine(binPath, "bin");
            //-- Please improve this if there is a better way
            //-- Also note that apps like webapps do not have a debug or release folder. So we would just return bin.
#if DEBUG
            if (Directory.Exists(Path.Combine(binPath, "Debug"))) 
                        binPath = Path.Combine(binPath, "Debug");
#else
            if (Directory.Exists(Path.Combine(binPath, "Release"))) 
                        binPath = Path.Combine(binPath, "Release");
#endif
        }
            return binPath;
    }
}

其他回答

注:组装。CodeBase在. net Core/中已弃用。NET 5+: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.codebase?view=net-5.0

最初的回答:

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

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);
    }
}

大会。Location属性有时会在使用NUnit时给你一些有趣的结果(其中程序集从临时文件夹运行),所以我更喜欢使用CodeBase,它以URI格式给你路径,然后是UriBuild。unescapedatasstring在开始时删除File://, GetDirectoryName将其更改为正常的windows格式。

我怀疑这里真正的问题是您的测试运行程序将您的程序集复制到不同的位置。在运行时没有办法知道程序集是从哪里复制的,但是您可能可以打开一个开关来告诉测试运行程序集从它所在的位置运行,而不是将它复制到影子目录。

当然,对于每个测试运行者,这样的切换可能是不同的。

您是否考虑过将XML数据作为资源嵌入到测试程序集中?

string path = Path.GetDirectoryName(typeof(DaoTests).Module.FullyQualifiedName);

ASP。网络,不管用。我在为什么AppDomain.CurrentDomain.BaseDirectory不包含asp.net应用程序中的“bin”中找到了一个更好的解决方案。它适用于Win Application和ASP。Net Web应用程序。

public string ApplicationPath
    {
        get
        {
            if (String.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath))
            {
                return AppDomain.CurrentDomain.BaseDirectory; //exe folder for WinForms, Consoles, Windows Services
            }
            else
            {
                return AppDomain.CurrentDomain.RelativeSearchPath; //bin folder for Web Apps 
            }
        }
    }

您可以通过获取bin路径 AppDomain.CurrentDomain.RelativeSearchPath