2

手動で指定した app.config ファイルから読み取りたい dll があります (dll は、Microsoft 管理コンソール スナップインであるネイティブ com dll の .net 拡張子であるため、mmc.exe.config はありません)。構成ファイルを開き、関連するグループとセクションを読んで、必要な設定を取得することができました。このような:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config"; 
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            ShowSectionGroupCollectionInfo(config.SectionGroups);
            ConfigurationSectionGroup group = config.SectionGroups["applicationSettings"];
            ClientSettingsSection section = group.Sections["Namespace.Properties.Settings"] as ClientSettingsSection;
            SettingElement sectionElement = section.Settings.Get("AllowedPlugins");

            SettingValueElement elementValue = sectionElement.Value;

設定は文字列コレクションと文字列です。そのようです:

<applicationSettings>
    <Namespace.Properties.Settings>
        <setting name="AllowedPlugins" serializeAs="Xml">
            <value>
                <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                    <string>Plugin1.Name</string>
                    <string>Plugin2.Name</string>
                    <string>Plugin3.Name</string>
                </ArrayOfString>
            </value>
        </setting>
        <setting name="blah" serializeAs="String">
            <value>sajksjaksj</value>
        </setting>
    </Namespace.Properties.Settings>
</applicationSettings>

少し kak を使用した方法で、これから文字列配列を作成できます。

List<String> values = new List<string>(elementValue.ValueXml.InnerText.Split(new string[]{" ",Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries ));

しかし、標準のapp.configファイルが読み取られるときと同じように、設定を読み取って正しいタイプのオブジェクトに変換できるという良い方法があるのではないかと思います。

あると教えてください...

4

2 に答える 2

6

私が過去にこれを行った方法は、カスタム構成セクションを使用し、セクションに を実装するIConfigurationSectionHandler ことです。構成セクション ハンドラー内ですべての解析を行う必要がありますが、構成セクション内の情報をアプリケーションで必要な形式で簡単に取得できます。このようなハンドラーを作成するプロセスを説明するハウツー記事が MSDN にあります。

以下は、私が使用した例です。

利用方法:

Dictionary<string,AdministrativeRole> roles
        = ConfigurationManager.GetSection("roles")
             as Dictionary<string,AdministrativeRole>;

web.config:

<configuration>
   <configSections>
      <section name="roles"
               type="com.example.RolesDefinitionHandler, com.example" />
   </configSections>
   <roles>
       <role name="Administrator" group="domain\group-name" />
       ...
   </roles>
</configuration>

コード:

public class RolesDefinitionHandler : IConfigurationSectionHandler
{
    private static readonly string ROLE_SECTION_NAME = "role";
    private static readonly string ROLE_SECTION_NAME_ATTRIBUTE = "name";
    private static readonly string ROLE_SECTION_GROUP_ATTRIBUTE = "group";

    private Dictionary<string, AdministrativeRole> ReadConfiguration( XmlNode section )
    {
        Dictionary<string, AdministrativeRole> roles = new Dictionary<string, AdministrativeRole>();
        foreach (XmlNode node in section.ChildNodes)
        {
            if (node.Name.Equals( ROLE_SECTION_NAME, StringComparison.InvariantCultureIgnoreCase ))
            {
                string name = (node.Attributes[ROLE_SECTION_NAME_ATTRIBUTE] != null) ? node.Attributes[ROLE_SECTION_NAME_ATTRIBUTE].Value.ToLower() : null;
                if (string.IsNullOrEmpty( name ))
                {
                    throw new ConfigurationErrorsException( "missing required attribute " + ROLE_SECTION_NAME_ATTRIBUTE );
                }

                string group = (node.Attributes[ROLE_SECTION_GROUP_ATTRIBUTE] != null) ? node.Attributes[ROLE_SECTION_GROUP_ATTRIBUTE].Value.ToLower() : null;
                if (string.IsNullOrEmpty( group ))
                {
                    throw new ConfigurationErrorsException( "missing required attribute " + ROLE_SECTION_GROUP_ATTRIBUTE );
                }

                if (roles.ContainsKey( name ))
                {
                    throw new ConfigurationErrorsException( "duplicate " + ROLE_SECTION_NAME + " for " + name );
                }

                roles.Add( name, new AdministrativeRole( name, group ) );
            }
            else
            {
                throw new ConfigurationErrorsException( "illegal node " + node.Name );
            }
        }

        return roles;
    }

    #region IConfigurationSectionHandler Members

    public object Create( object parent, object configContext, System.Xml.XmlNode section )
    {
        return ReadConfiguration( section );
    }

    #endregion
}
于 2009-02-10T14:58:33.190 に答える