如何获取代码所在的程序集的路径?

丶灬走出姿态 提交于 2019-12-21 20:34:13

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

有没有办法获取当前代码所在的程序集的路径? 我不希望调用程序集的路径,而只是包含代码的路径。

基本上,我的单​​元测试需要读取一些相对于dll的xml测试文件。 我希望该路径始终正确解析,而不管测试dll是从TestDriven.NET,MbUnit GUI还是其他版本运行。

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

我的测试库位于

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

我想走这条路:

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

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

  • Environment.CurrentDirectory给出c:\\ Program Files \\ MbUnit

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

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


#1楼

这应该工作:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
Assembly asm = Assembly.GetCallingAssembly();
String path = Path.GetDirectoryName(new Uri(asm.EscapedCodeBase).LocalPath);

string strLog4NetConfigPath = System.IO.Path.Combine(path, "log4net.config");

我正在使用它来部署DLL文件库以及一些配置文件(这是从DLL文件中使用log4net)。


#2楼

这就是我想出的。 在Web项目之间,进行单元测试(nunit和resharper测试运行程序) ; 我发现这对我有用。

我一直在寻找代码来检测内部版本的配置, Debug/Release/CustomName 。 las, #if DEBUG因此,如果有人可以改善它

随时进行编辑和改进。

正在获取应用程序文件夹 。 对于Web根目录很有用,unittests用于获取测试文件的文件夹。

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

#3楼

据我所知,大多数其他答案都有一些问题。

对于基于磁盘(而不是基于Web的),非GACed程序集 ,执行此操作的正确方法是使用当前正在执行的程序集的CodeBase属性。

这将返回一个URL( file:// )。 不用搞乱字符串操作UnescapeDataString ,可以利用UriLocalPath属性以最小的麻烦进行转换。

var codeBaseUrl = Assembly.GetExecutingAssembly().CodeBase;
var filePathToCodeBase = new Uri(codeBaseUrl).LocalPath;
var directoryPath = Path.GetDirectoryName(filePathToCodeBase);

#4楼

Web应用程序?

Server.MapPath("~/MyDir/MyFile.ext")

#5楼

与John的答案相同,但扩展方法略为冗长。

public static string GetDirectoryPath(this Assembly assembly)
{
    string filePath = new Uri(assembly.CodeBase).LocalPath;
    return Path.GetDirectoryName(filePath);            
}

现在您可以执行以下操作:

var localDir = Assembly.GetExecutingAssembly().GetDirectoryPath();

或者,如果您喜欢:

var localDir = typeof(DaoTests).Assembly.GetDirectoryPath();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!