|
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections.Specialized;
using System.Globalization;
using System.Configuration;
using System.IO;
namespace Microsoft.Practices.EnterpriseLibrary.Configuration
{
internal class ConfigurationFilePicker
{
private ConfigurationFilePicker()
{
}
public static string Lookup(string configurationFile, string machineConfigurationFile)
{
// EntLibConfig tool is allowed to load any configuration file.
// So check whether EntLibConfig is the process executable.
// Hint: If GetEntryAssembly() returns null, there is no process executable in the default
// application domain (e.g. for ASP.NET web applications). In this case, we are clearly not
// running EntLibConfig.
if (Assembly.GetEntryAssembly() != null)
{
if (string.Compare(Assembly.GetEntryAssembly().GetName().Name, "EntLibConfig", true, CultureInfo.InvariantCulture) == 0)
return configurationFile;
}
// Machine.config file must not be loaded as main configuration file.
if (string.Compare(configurationFile, machineConfigurationFile, true, CultureInfo.InvariantCulture) == 0)
throw new ConfigurationErrorsException("Loading machine.config as main configuration file is not supported here.");
// Retrieve entLibConfigFile key from appsettings.
NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings;
string entLibConfigFile = appSettings.Get("entLibConfigFile");
if (entLibConfigFile == null)
throw new ConfigurationErrorsException("Unable to find entLibConfigFile key in appSettings section.");
// Retrieve path of application / web configuration file.
string appConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
string appConfigPath = Path.GetDirectoryName(appConfigFile);
if (!appConfigPath.EndsWith(@"\")) appConfigPath += @"\";
// entLibConfigFile is appended to appConfigPath.
if (entLibConfigFile.StartsWith(@"\")) entLibConfigFile = entLibConfigFile.TrimStart(@"\".ToCharArray());
return appConfigPath + entLibConfigFile;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Michael is working with programming technologies since the beginning of the nineties. His main interest is in Microsoft .NET and BizTalk Server.