如何获取运行类库的物理文件名

本文关键字:类库 文件名 运行 何获取 获取 | 更新日期: 2023-09-27 18:03:08

我正在尝试创建一个类库,它将根据其物理文件名加载不同的文件。

。如果物理文件名为"test.dll",它将从"test.dll"加载其设置。如果文件被复制并重命名为"copy.dll",那么它将从"copy.config"中加载其设置....

System.Diagnostics.Process.GetCurrentProcess(); 
//doesn't work as I'm loading the dll's dynamically into a console application and calling their methods.

任何想法?

如何获取运行类库的物理文件名

string filePath = typeof(SomeAssemblyMemberType).Assembly.CodeBase;

CodeBase属性返回加载的包含清单文件的绝对路径,或者如果使用Load方法加载程序集,则返回加载器方法所在程序集的路径。

例如,如果您在DLL中的类对象中有以下代码:

public class Grace
{
    public Grace() {}
    public string AbsoluteFileName
    {
        get {
            return this.GetType().Assembly.CodeBase;
        }
    }
}

Use Assembly:

 string filePath = System.Reflection.Assembly.GetCallingAssembly().Location;