C#:读取和修改另一个应用程序的app.config文件中的设置 [英] C#: Read and modify settings in another application's app.config file

查看:897
本文介绍了C#:读取和修改另一个应用程序的app.config文件中的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行着许多相互通信的应用程序,但是这些应用程序都没有自己的用户界面。我有一个系统控制台应用程序,它充当系统的用户界面(即相互交谈的一组应用程序)。

I have a number of applications running which communicate with each other but none of these applications have their own user interface. I have a system console application which acts as a user interface for the system (i.e. the set of applications which all talk to each other).

我希望能够使用系统控制台读取和修改每个非GUI应用程序的配置。

每个应用程序具有使用Visual Studio设置GUI创建的app.config文件。设置全部在应用程序范围内,这将导致一个app.config文件看起来像这样:

I would like to be able to use the system console to read and modify the configuration of each of the non-gui apps.
Each app has an app.config file created using the Visual Studio Settings GUI. The settings are all in application scope, which results in an app.config file which looks a bit like this:

<?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>

我尝试使用以下内容读取配置设置的代码:

I have tried using the following code to read the configuration settings:

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对象时,我发现它不包含任何设置。

However on closer inspection of the config.AppSettings object, I find that it contains no settings.

我在做什么错?我使用错误的方法来读取配置文件吗?这种方法是否最适合另一种配置文件?

What am I doing wrong? Am I using the wrong method to read the config file? Is this method best for a different sort of config file?

推荐答案

可以将配置文件用作XML,然后使用XPath更改值:

Its possible to use the config file as XML and then use XPath to change values:

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的可能路径:

Possible path to Conf1:

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

这篇关于C#:读取和修改另一个应用程序的app.config文件中的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆