app.config files and MMC snap-in

R

richlm

I'm developing a snap-in for MMC 3.0 using .Net framework 2.0 and the new
MMC class library.

I need to store some configuration settings, ideally in a .config file
associated with my snap-in dll.

A couple of possible solutions:
1. Deploy an mmc.exe.config to %windir%\System32 - not really a workable
solution.

2. Open a configuration file explicitly as a "Configuration" instance using
e.g. ConfigurationManager.OpenMappedExeConfiguration. That's almost OK - but
the config file either needs to be in System32 or we have to hard-code a
path (or maybe get the path from Assembly.CodeBase).

The same issue would apply to other non-standard dll hosts.
Can anyone recommend a better approach?

Richard.
 
R

richlm

Option 2 seems to work OK so I'll post some code snippets for the benefit of others.
Still if anyone has additional comments please post.

private static List<System.Configuration.Configuration> _configurations = null;

....

// check the default configuration

object config = ConfigurationManager.GetSection(sectionName);

if (config == null)

{

// check the alternative configurations

if (_configurations == null)

LoadConfigurations();

foreach (System.Configuration.Configuration cfg in _configurations)

{

config = cfg.GetSection(sectionName);

if (config != null)

break;

}

}

....



private static void LoadConfigurations()

{

_configurations = new List<System.Configuration.Configuration>();

string path = Assembly.GetCallingAssembly().CodeBase;

string folder = Path.GetDirectoryName(path);

string processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;

ExeConfigurationFileMap map = new ExeConfigurationFileMap();

// check for [host-exe].config in the same folder as the calling assembly

string configFile = Path.Combine(folder, processName + ".exe.config");

if (File.Exists(configFile))

{

map.ExeConfigFilename = configFile;

_configurations.Add(ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None));

}

// check for mycompanyname.config in the same folder as the calling assembly

configFile = Path.Combine(folder, "mycompanyname.config");

if (File.Exists(configFile))

{

map.ExeConfigFilename = configFile;

_configurations.Add(ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None));

}

}
 
E

Eric Voskuil \(MVP\)

richlm,

See:

http://msdn.microsoft.com/library/d...ngsAttribute_ConfigurationFile.asp?frame=true

Eric
Option 2 seems to work OK so I'll post some code snippets for the benefit of others.
Still if anyone has additional comments please post.

private static List<System.Configuration.Configuration> _configurations = null;

...

// check the default configuration

object config = ConfigurationManager.GetSection(sectionName);

if (config == null)

{

// check the alternative configurations

if (_configurations == null)

LoadConfigurations();

foreach (System.Configuration.Configuration cfg in _configurations)

{

config = cfg.GetSection(sectionName);

if (config != null)

break;

}

}

...



private static void LoadConfigurations()

{

_configurations = new List<System.Configuration.Configuration>();

string path = Assembly.GetCallingAssembly().CodeBase;

string folder = Path.GetDirectoryName(path);

string processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;

ExeConfigurationFileMap map = new ExeConfigurationFileMap();

// check for [host-exe].config in the same folder as the calling assembly

string configFile = Path.Combine(folder, processName + ".exe.config");

if (File.Exists(configFile))

{

map.ExeConfigFilename = configFile;

_configurations.Add(ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None));

}

// check for mycompanyname.config in the same folder as the calling assembly

configFile = Path.Combine(folder, "mycompanyname.config");

if (File.Exists(configFile))

{

map.ExeConfigFilename = configFile;

_configurations.Add(ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None));

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top