2

相互に通信するアプリケーションをいくつか実行していますが、これらのアプリケーションには独自のユーザーインターフェイスがありません。システムのユーザーインターフェイスとして機能するシステムコンソールアプリケーション(つまり、すべてが相互に通信するアプリケーションのセット)があります。

システムコンソールを使用して、GUI以外の各アプリの構成を読み取って変更できるようにしたいと思います。
各アプリには、VisualStudio設定GUIを使用して作成されたapp.configファイルがあります。設定はすべてアプリケーションスコープ内にあり、その結果、app.configファイルは次のようになります。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="ExternalConfigReceiver.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<applicationSettings>
    <ExternalConfigReceiver.Properties.Settings>
        <setting name="Conf1" serializeAs="String">
            <value>3</value>
        </setting>
        <setting name="Conf2" serializeAs="String">
            <value>4</value>
        </setting>
    </ExternalConfigReceiver.Properties.Settings>
</applicationSettings>

次のコードを使用して構成設定を読み取ってみました。

System.Configuration.ExeConfigurationFileMap fileMap = new   System.Configuration.ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "PATH_TO_THE_FOLDER\\app.config";

System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None);

someVariable =  config.AppSettings.Settings["Conf1"];
someVariable2 = config.AppSettings.Settings["Conf2"];

ただし、config.AppSettingsオブジェクトを詳しく調べると、設定が含まれていないことがわかります。

私は何が間違っているのですか?設定ファイルを読み取るために間違った方法を使用していますか?この方法は、別の種類の構成ファイルに最適ですか?

4

2 に答える 2

4

構成ファイルを XML として使用し、XPath を使用して値を変更することができます。

using (TransactionScope transactionScope = new TransactionScope())
{
    XmlDocument configFile = new XmlDocument();

    configFile.Load("PathToConfigFile");

    XPathNavigator fileNavigator = configFile.CreateNavigator();

    // User recursive function to get to the correct node and set the value
    WriteValueToConfigFile(fileNavigator, pathToValue, newValue);

    configFile.Save("PathToConfigFile");

    // Commit transaction
    transactionScope.Complete();
}

private void WriteValueToConfigFile(XPathNavigator fileNavigator, string remainingPath, string newValue)
{
    string[] splittedXPath = remainingPath.Split(new[] { '/' }, 2);
    if (splittedXPath.Length == 0 || String.IsNullOrEmpty(remainingPath))
    {
        throw new Exception("Path incorrect.");
    }

    string xPathPart = splittedXPath[0];
    XPathNavigator nodeNavigator = fileNavigator.SelectSingleNode(xPathPart);

    if (splittedXPath.Length > 1)
    {
        // Recursion
        WriteValueToConfigFile(nodeNavigator, splittedXPath[1], newValue);
    }
    else
    {
        nodeNavigator.SetValue(newValue ?? String.Empty);
    }
}

Conf1 への可能なパス:

"configuration/applicationSettings/ExternalConfigReceiver.Properties.Settings/setting[name=\"Conf1\"]/value"

于 2012-12-18T11:11:03.823 に答える
0

私の知る限り、 System.Configuration.Configuration を使用して他のアプリケーションの構成ファイルにアクセスすることはできません

それらはxmlファイルであり、xml名前空間を使用してそれらと対話できます

于 2012-12-18T10:47:26.413 に答える