So, today i learned the hard way that .NET class library apps don't intrinsically support .config files. after about 4 hours of head scratching i came up w/ the following bit of code that will dynamically load a config file with the same name as the assembly loading it:
internal void GetConfigurationFilePath()
{
DirectoryInfo currentDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\");
_pathToWorkingDirectory = currentDir.FullName;
//loop through all assemblies currently loaded
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
string asmName = asm.CodeBase;
int index = asm.CodeBase.LastIndexOf(@"/");
string part = asmName.Substring(index + 1);
//now compare that substring to the name of any config files found in the current directory
foreach (FileInfo file in currentDir.GetFiles ("*.config"))
{
if (part.Trim().ToLower() + ".config" == file.Name.Trim().ToLower())
{
_configFileName = file.FullName;
}
}
}
}it's actually quite simple once you look at it. so my question to the engineers at ms is wtf??? class libraries should be able to have dynamic config files just like their big brother .EXE's. moral of the story? not even .NET is perfect :-)
cheerio
j



No comments yet