关于C#:如何获取代码所在程序集的路径?

How do I get the path of the assembly the code is in?

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

基本上,我的单元测试需要读取一些相对于DLL的XML测试文件。无论测试dll是从testsdriven.net、mbunit gui还是其他什么地方运行,我都希望路径始终正确解析。

编辑:人们似乎误解了我的要求。

我的测试库位于Say

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

我想知道这条路:

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

到目前为止,当我从MBUnit GUI运行时,这三个建议使我失败:

  • Environment.CurrentDirectory提供C:Program FilesmbUnit

  • System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location提供C:文档和设置乔治本地设置 emp……daotests.dll

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


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

1
2
3
4
5
6
7
8
9
10
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);
    }
}

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


这有帮助吗?

1
2
3
4
5
//get the full location of the assembly with DaoTests in it
string fullPath = System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location;

//get the folder that's in
string theDirectory = Path.GetDirectoryName( fullPath );

就这么简单:

1
var dir = AppDomain.CurrentDomain.BaseDirectory;

和约翰的答案一样,只是稍微少了一点冗长的扩展方法。

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

现在你可以做到:

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

或者如果你喜欢:

1
var localDir = typeof(DaoTests).Assembly.GetDirectoryPath();


在使用codebase和unc网络共享时,唯一对我有效的解决方案是:

1
System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);

它也适用于正常的URI。


除非程序集被卷影复制,否则这应该可以工作:

1
string path = System.Reflection.Assembly.GetExecutingAssembly().Location

这个怎么样:

1
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

我怀疑这里真正的问题是您的测试运行程序正在将您的程序集复制到其他位置。在运行时,无法确定程序集是从何处复制的,但您可能可以翻转开关,告诉测试运行程序从何处运行程序集,而不是将其复制到影子目录。

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

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


1
AppDomain.CurrentDomain.BaseDirectory

使用MBUnit GUI。


这是John Sibly代码的vb.net端口。VisualBasic不区分大小写,因此他的几个变量名与类型名冲突。

1
2
3
4
5
6
7
8
Public Shared ReadOnly Property AssemblyDirectory() As String
    Get
        Dim codeBase As String = Assembly.GetExecutingAssembly().CodeBase
        Dim uriBuilder As New UriBuilder(codeBase)
        Dim assemblyPath As String = Uri.UnescapeDataString(uriBuilder.Path)
        Return Path.GetDirectoryName(assemblyPath)
    End Get
End Property

1
2
3
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var assemblyPath = assembly.GetFiles()[0].Name;
var assemblyDir = System.IO.Path.GetDirectoryName(assemblyPath);

这个怎么样…

1
string ThisdllDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

那就砍下你不需要的东西吧


我一直在使用assembly.codebase而不是location:

1
2
3
4
5
6
7
8
9
Assembly a;
a = Assembly.GetAssembly(typeof(DaoTests));
string s = a.CodeBase.ToUpper(); // file:///c:/path/name.dll
Assert.AreEqual(true, s.StartsWith("FILE://"),"CodeBase is" + s);
s = s.Substring(7, s.LastIndexOf('/') - 7); // 7 ="file://"
while (s.StartsWith("/")) {
    s = s.Substring(1, s.Length - 1);
}
s = s.Replace("/","\");

它一直在工作,但我不再确定它是100%正确的。http://blogs.msdn.com/suzbook/archive/2003/06/26/assembly-codebase-vs-assembly-location.aspx上的页面显示:

"代码库是找到文件的位置的URL,而位置是实际加载文件的路径。例如,如果程序集是从Internet下载的,则其代码基可能以"http://"开头,但其位置可能以"c:"开头。如果文件是卷影复制的,则位置将是卷影复制目录中文件副本的路径。也很好地知道,不能保证为GAC中的程序集设置代码基。但是,将始终为从磁盘加载的程序集设置位置。"

您可能希望使用代码库而不是位置。


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

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

这将返回一个URL(File://)。这可以通过利用UriLocalPath属性,在不干扰字符串操作或UnescapeDataString的情况下进行转换。

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


这些年来,没有人真的提到过这个。我从很棒的ApprovalTests项目中学到了一个技巧。技巧是使用程序集中的调试信息查找原始目录。

这不会在发布模式下工作,也不会在启用优化的情况下工作,也不会在与编译时不同的计算机上工作。

但这将为您提供与调用源代码文件的位置相关的路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static class PathUtilities
{
    public static string GetAdjacentFile(string relativePath)
    {
        return GetDirectoryForCaller(1) + relativePath;
    }
    public static string GetDirectoryForCaller()
    {
        return GetDirectoryForCaller(1);
    }


    public static string GetDirectoryForCaller(int callerStackDepth)
    {
        var stackFrame = new StackTrace(true).GetFrame(callerStackDepth + 1);
        return GetDirectoryForStackFrame(stackFrame);
    }

    public static string GetDirectoryForStackFrame(StackFrame stackFrame)
    {
        return new FileInfo(stackFrame.GetFileName()).Directory.FullName + Path.DirectorySeparatorChar;
    }
}

当前存在的目录。

1
Environment.CurrentDirectory;  // This is the current directory of your application

如果用build复制.xml文件,应该找到它。

1
2
3
4
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(SomeObject));

// The location of the Assembly
assembly.Location;

当开发人员可以更改代码以包含所需的代码段时,所有建议的答案都有效,但是如果您想在不更改任何代码的情况下执行此操作,则可以使用Process Explorer。

它将列出系统上所有正在执行的DLL,您可能需要确定正在运行的应用程序的进程ID,但这通常并不太困难。

我已经写了一个完整的描述如何在II中为一个dll做这个-http://nodogmablog.bryanhogan.net/2016/09/locating-and-checking-an-executing-dll-on-a-running-web-server/


在Windows窗体应用程序中,您只需使用Application.StartupPath

但是对于DLL和控制台应用程序来说,代码更难记住…

1
2
3
4
5
string slash = Path.DirectorySeparatorChar.ToString();
string root = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

root += slash;
string settingsIni = root +"settings.ini"

你可以通过AppDomain.CurrentDomain.RelativesearchPath


如果路径包含""符号,则会得到不正确的目录。所以我使用了对john sibly答案的修改,它是uribuilder.path和uribuilder.fragment的组合:

1
2
3
4
5
6
7
8
9
10
11
12
public static string AssemblyDirectory
{
    get
    {
        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        //modification of the John Sibly answer    
        string path = Uri.UnescapeDataString(uri.Path.Replace("/","\") +
          uri.Fragment.Replace("
/","\"));
        return Path.GetDirectoryName(path);
     }
}

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

我以前在江户有过同样的行为。默认情况下,NUnit会将程序集复制到temp目录中。您可以在NUnit设置中更改此行为:

enter image description here

可能TestDriven.NETMbUnitGUI具有相同的设置。


我发现我的解决方案足以找到那个地点。

1
var executingAssembly = new FileInfo((Assembly.GetExecutingAssembly().Location)).Directory.FullName;

这应该有效:

1
2
3
4
5
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)。


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

我一直在寻找代码来检测构建的配置,Debug/Release/CustomName。唉,江户记1〔6〕。所以如果有人能改进的话!

随时编辑和改进。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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文件夹:用于使用反射执行程序集。如果由于生成属性而将文件复制到那里。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
    }
}

我使用这个来获取bin目录的路径:

1
2
var i = Environment.CurrentDirectory.LastIndexOf(@"");
var path = Environment.CurrentDirectory.Substring(0,i);

你得到这个结果:

"c:\users
icooley\documents\visual studio
2010\Projects\Windows_Test_Project\Windows_Test_Project\bin"


Web应用程序?

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

推荐阅读

    linux读取码值命令?

    linux读取码值命令?,系统,工作,地址,证书,命令,工具,档案,文件,设计,信息,基

    linux使用命令的方法?

    linux使用命令的方法?,系统,信息,工具,标准,数据,命令,左下角,目录,文件夹,

    linux常用的命令路径?

    linux常用的命令路径?,地址,工作,基础,网络,管理,系统,信息,命令,企业,服务,l

    linux切换路径命令行?

    linux切换路径命令行?,系统,工作,名称,地址,目录,命令,工具,密码,一致,管理,

    脚本调用linux命令?

    脚本调用linux命令?,代码,系统,工作,底部,脚本,位置,环境,行用,官网,标准,typ

    调用函数命令linux?

    调用函数命令linux?,系统,管理,网络,通用,统一,观察,地址,代码,设备,地方,怎

    linux改变命令的路径?

    linux改变命令的路径?,数字,第一,目录,工作,一致,名称,系统,命令,文件夹,代

    linux中显示路径命令?

    linux中显示路径命令?,系统,地址,工作,信息,时间,命令,数据,网络,路径,名字,l

    linux保存路径命令行?

    linux保存路径命令行?,状态,地址,电脑,档案,系统,底部,软件,密码,命令,文件,l

    linux相对路径命令?

    linux相对路径命令?,工作,名字,路径,目录,管理,信息,系统,位置,名称,基础,lin

    linux多路径查看命令?

    linux多路径查看命令?,设备,软件,网络,工具,系统,情况,下降,公司,名称,大幅,

    linux内核总调用命令?

    linux内核总调用命令?,工作,地址,系统,信息,管理,策略,命令,目录,时间,基础,

    linux路径命令解释?

    linux路径命令解释?,系统,信息,设备,数据,工具,命令,文件,标准,发行,时间,lin

    linux编程调用命令?

    linux编程调用命令?,系统,标准,管理,工作,基础知识,情况,环境,设备,基础,首

    linux相对路径和命令?

    linux相对路径和命令?,地址,工作,信息,数据,系统,命令,文件,目录,时间,路径,l

    linux显示路径命令?

    linux显示路径命令?,系统,信息,工作,数据,名称,位置,标准,环境,时间,命令,lin

    linux拷贝路径命令行?

    linux拷贝路径命令行?,系统,文件,命令,目录,名称,源文件,文件夹,目标,路径,

    linux路径截取命令?

    linux路径截取命令?,标准,图片,命令,环境,工具,信息,软件,系统,文件,目录,Lin

    linux命令窗口调用?

    linux命令窗口调用?,系统,工具,首页,终端,命令,数据,盘中,代码,密码,快捷键,

    linux挂载命令路径?

    linux挂载命令路径?,服务,信息,系统,电脑,情况,工具,网络,命令,磁盘,目录,Lin