Reading of specific Configuration file

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

 

The configuration file can be usually read by using of ConfigurationManager. For example, to load the application configuration file following
code can be used:

Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

 

Depending of what configuration file you want to load, one of following values can be used:

public enum ConfigurationUserLevel

{

    None = 0,

    PerUserRoaming = 10,

    PerUserRoamingAndLocal = 20,

}

 

But, what if you want load the configuration file form same specific location? Following code show how to do this:

 

public static Configuration OpenSpecificConfigurationFile(string configPath)

{

    if (string.IsNullOrEmpty(configPath))

    {

        throw new ApplicationException("TODO");

    }

 

    System.Configuration.Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();

    if (File.Exists(configPath) == false)

    {

        throw new ApplicationException("TODO");

    }

 

    try

    {

        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

        fileMap.MachineConfigFilename = machineConfig.FilePath;

        fileMap.ExeConfigFilename = configPath;

       
        Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(fileMap,
        ConfigurationUserLevel.None);

 

        return cfg;

    }

    catch (Exception exception)

    {

        throw exception;

    }

}


Posted Sep 16 2007, 08:33 PM by Damir Dobric
developers.de is a .Net Community Blog powered by daenet GmbH.