Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / XML

Using the Enterprise Library (June 2005) with Visual Studio 2005

Rate me:
Please Sign up or sign in to vote.
3.76/5 (21 votes)
5 Feb 2015CPOL8 min read 139.7K   591   57  
How to customize the Enterprise Library (June 2005) for Visual Studio 2005
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Germany Germany
Michael is working with programming technologies since the beginning of the nineties. His main interest is in Microsoft .NET and BizTalk Server.

Comments and Discussions