0

I'm trying to create a class library which will load different files depending on its physical file name.

i.e. if physical file name is called "test.dll" it will load its settings from "test.config", if the file is duplicated and renamed to "copy.dll" then it will load its settings from "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.

Any ideas?

zaza
  • 892
  • 1
  • 18
  • 37
  • 1
    Are you looking for something along the lines of the System.Reflection.Assembly class? the GetAssembly method will help or the GetCallingAssembly or GetExecutingAssembly – m.t.bennett Mar 08 '13 at 23:16
  • 1
    What .NET object are using the read the config from inside the DLL? Because if you are using the default ConfigurationManager is will only read the .exe.config file. – Black Frog Mar 09 '13 at 01:41

2 Answers2

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

CodeBase property returns absolute path to the loaded manifest-containing file, or if assembly was loaded using Load method, returns path to assembly in which loader method is.

For example if you had the following code in a class object in your DLL:

public class Grace
{
    public Grace() {}

    public string AbsoluteFileName
    {
        get {
            return this.GetType().Assembly.CodeBase;
        }
    }
}
Black Frog
  • 11,595
  • 1
  • 35
  • 66
Nikola Radosavljević
  • 6,871
  • 32
  • 44
0

Use Assembly:

 string filePath = System.Reflection.Assembly.GetCallingAssembly().Location;
Jens Meinecke
  • 2,904
  • 17
  • 20