Home All Groups Group Topic Archive Search About

app.config files and MMC snap-in

Author
31 Mar 2006 12:12 PM
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.

Author
31 Mar 2006 3:15 PM
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));

    }

}


Show quote
"richlm" <richlm@nospam.nospam> wrote in message news:eremtsLVGHA.4792@TK2MSFTNGP14.phx.gbl...
> 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.
>
>
Author
1 Apr 2006 7:18 PM
Eric Voskuil (MVP)
richlm,

See:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/managedMMC_MRef/html/P_Microsoft_ManagementConsole_SnapInSettingsAttribute_ConfigurationFile.asp?frame=true

Eric
  "richlm" <richlm@nospam.nospam> wrote in message news:%23kWo7TNVGHA.4740@TK2MSFTNGP14.phx.gbl...
  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));

      }

  }


Show quote
  "richlm" <richlm@nospam.nospam> wrote in message news:eremtsLVGHA.4792@TK2MSFTNGP14.phx.gbl...
  > 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.
  >
  >

AddThis Social Bookmark Button