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

时间:2013-03-08 23:09:06

标签: c# file system.diagnostics

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

即。如果物理文件名称为“test.dll”,它将从“test.config”加载其设置,如果文件被复制并重命名为“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.

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

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

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

例如,如果DLL中的类对象中包含以下代码:

public class Grace
{
    public Grace() {}

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

答案 1 :(得分:0)

使用汇编:

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