posted on Monday, November 22, 2004 1:23 PM by beliwan

Reading UIPConfigSettings from alternative location

I was looking for a way to separate the UIP configuration settings from the default configuration file. Finally come up with the following solution. To use it you have to change the UIPConfiguration class.

By adding the alternativeConfigFile attribute to the uipConfiguration section, you are able to use the following function to get the configuration

configSections>
section alternativeConfigFile="uipConfiguration.config" name="uipConfiguration" type="Microsoft.ApplicationBlocks.UIProcess.UIPConfigHandler, Microsoft.ApplicationBlocks.UIProcess, Version=1.0.1.0,Culture=neutral,PublicKeyToken=null" />
configSections>

Add the following code to the try section of the getter :

_currentConfig = GetSettingsFromAlternativeLocation();
if ( _currentConfig == null ) _currentConfig = (UIPConfigSettings)ConfigurationSettings.GetConfig( UipConfigSection );

Finally implement the following private function:

private static UIPConfigSettings GetSettingsFromAlternativeLocation()

{

//Retrieve location of EntryAssembly

string entryAssemblyLocation = System.Reflection.Assembly.GetEntryAssembly().Location;

string usedConfigFile = entryAssemblyLocation + ".config";

//Load file in XmlDocument

XmlDocument configurationFile = new XmlDocument();

configurationFile.Load(usedConfigFile);

//Get uipSection

XmlNode uipSectionNode = configurationFile.SelectSingleNode("/configuration/configSections/section[@name='uipConfiguration']");

//Read alternativeConfigFile location

string alternativeConfigFile = ((XmlElement)uipSectionNode).GetAttribute("alternativeConfigFile");

//If not exist return null, to handle default uipConfiguration section

if ( alternativeConfigFile == string.Empty ) return null;

//If filelocation exist then use it, else Combine it with DirectoryName of configfile

if ( !File.Exists(alternativeConfigFile) )

{

FileInfo fileInfo = new FileInfo(usedConfigFile);

alternativeConfigFile = Path.Combine(fileInfo.DirectoryName, alternativeConfigFile);

}

//reuse XmlDocument and load alternative configuration file with UIP settings

configurationFile.Load(alternativeConfigFile);

XmlNode uipConfigurationNode = configurationFile.SelectSingleNode("//uipConfiguration");

if ( uipConfigurationNode != null )

{

UIPConfigHandler uipConfigHandler = new UIPConfigHandler();

return (UIPConfigSettings)uipConfigHandler.Create(null,null, uipConfigurationNode);

}

else return null;

}

Comments