使用配置管理器加载 app.config 文件后应用程序设置未显示

App Settings not showing up after loading a app.config file with configuration manager

好的,我有一个如下所示的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MDCHartfordMVVM.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <connectionStrings>
        <add name="MDCHartfordMVVM.Properties.Settings.DBConnection"
            connectionString="Data Source=.\sqlexpress;Initial Catalog=TPS;Persist Security Info=True;User ID=sa;Password=service#123"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <applicationSettings>
        <MDCHartfordMVVM.Properties.Settings>
            <setting name="OPCServer" serializeAs="String">
                <value>Kepware.KepserverEX.v5</value>
            </setting>
            <setting name="OPCGroup" serializeAs="String">
                <value>Group1</value>
            </setting>
            <setting name="OPCUpdateRate" serializeAs="String">
                <value>5000</value>
            </setting>
            <setting name="OPCDeadband" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="HeartBeat" serializeAs="String">
                <value>TPS.KSS.HeartBeat</value>
            </setting>
            <setting name="TLICNumber" serializeAs="String">
                <value>TPS.KSS.TLICNumber</value>
            </setting>
            <setting name="TruckIDNumber" serializeAs="String">
                <value>TPS.KSS.TruckIDNumber</value>
            </setting>
            <setting name="TruckCompany" serializeAs="String">
                <value>TPS.KSS.TruckCompany</value>
            </setting>
            <setting name="TruckWidth" serializeAs="String">
                <value>TPS.KSS.TruckWidth</value>
            </setting>
            <setting name="TruckLength" serializeAs="String">
                <value>TPS.KSS.TruckLength</value>
            </setting>
            <setting name="TruckDepth" serializeAs="String">
                <value>TPS.KSS.TruckDepth</value>
            </setting>
            <setting name="TruckVolume" serializeAs="String">
                <value>TPS.KSS.TruckVolume</value>
            </setting>
            <setting name="TruckTare" serializeAs="String">
                <value>TPS.KSS.TruckTare</value>
            </setting>
            <setting name="TruckMaxGross" serializeAs="String">
                <value>TPS.KSS.TruckMaxGross</value>
            </setting>
            <setting name="TruckType" serializeAs="String">
                <value>TPS.KSS.TruckType</value>
            </setting>
            <setting name="Mt_Pos1_Lev1" serializeAs="String">
                <value>TPS.KSS.Mt_Pos1_Lev1</value>
            </setting>
            <setting name="Mt_Pos1_Lev2" serializeAs="String">
                <value>TPS.KSS.Mt_Pos1_Lev2</value>
            </setting>
            <setting name="Mt_Pos1_Lev3" serializeAs="String">
                <value>TPS.KSS.Mt_Pos1_Lev3</value>
            </setting>
            <setting name="Mt_Pos2_Lev1" serializeAs="String">
                <value>TPS.KSS.Mt_Pos2_Lev1</value>
            </setting>
            <setting name="Mt_Pos2_Lev2" serializeAs="String">
                <value>TPS.KSS.Mt_Pos2_Lev2</value>
            </setting>
            <setting name="Mt_Pos2_Lev3" serializeAs="String">
                <value>TPS.KSS.Mt_Pos2_Lev3</value>
            </setting>
            <setting name="Mt_Pos3_Lev1" serializeAs="String">
                <value>TPS.KSS.Mt_Pos3_Lev1</value>
            </setting>
            <setting name="Mt_Pos3_Lev2" serializeAs="String">
                <value>TPS.KSS.Mt_Pos3_Lev2</value>
            </setting>
            <setting name="Mt_Pos3_Lev3" serializeAs="String">
                <value>TPS.KSS.Mt_Pos3_Lev3</value>
            </setting>
            <setting name="Fl_Pos1_Lev1" serializeAs="String">
                <value>TPS.KSS.Fl_Pos1_Lev1</value>
            </setting>
            <setting name="Fl_Pos1_Lev2" serializeAs="String">
                <value>TPS.KSS.Fl_Pos1_Lev2</value>
            </setting>
            <setting name="Fl_Pos1_Lev3" serializeAs="String">
                <value>TPS.KSS.Fl_Pos1_Lev3</value>
            </setting>
            <setting name="Fl_Pos2_Lev1" serializeAs="String">
                <value>TPS.KSS.Fl_Pos2_Lev1</value>
            </setting>
            <setting name="Fl_Pos2_Lev2" serializeAs="String">
                <value>TPS.KSS.Fl_Pos2_Lev2</value>
            </setting>
            <setting name="Fl_Pos2_Lev3" serializeAs="String">
                <value>TPS.KSS.Fl_Pos2_Lev3</value>
            </setting>
            <setting name="Fl_Pos3_Lev1" serializeAs="String">
                <value>TPS.KSS.Fl_Pos3_Lev1</value>
            </setting>
            <setting name="Fl_Pos3_Lev2" serializeAs="String">
                <value>TPS.KSS.Fl_Pos3_Lev2</value>
            </setting>
            <setting name="Fl_Pos3_Lev3" serializeAs="String">
                <value>TPS.KSS.Fl_Pos3_Lev3</value>
            </setting>
            <setting name="TruckLoadPermissive" serializeAs="String">
                <value>TPS.KSS.TrkLoadPermissive</value>
            </setting>
            <setting name="TruckPositionTarget" serializeAs="String">
                <value>TPS.KSS.TrkPositionTarget</value>
            </setting>
            <setting name="TruckLoadPermissivePlcAsh" serializeAs="String">
                <value>TPS.KSS.TrkLoadPermissivePlcAsh</value>
            </setting>
            <setting name="LoadComplete_PlcAsh" serializeAs="String">
                <value>TPS.KSS.LoadComplete_PlcAsh</value>
            </setting>
            <setting name="TruckPresent" serializeAs="String">
                <value>TPS.KSS.TrkPresent</value>
            </setting>
            <setting name="TruckLoading" serializeAs="String">
                <value>TPS.KSS.TruckLoading</value>
            </setting>
            <setting name="TruckAmountLoaded" serializeAs="String">
                <value>TPS.KSS.TrkAmountLoaded</value>
            </setting>
            <setting name="PEPos1_1" serializeAs="String">
                <value>TPS.KSS.PEPos1_1</value>
            </setting>
            <setting name="PEPos2_1" serializeAs="String">
                <value>TPS.KSS.PEPos2_1</value>
            </setting>
            <setting name="PEPos3_1" serializeAs="String">
                <value>TPS.KSS.PEPos3_1</value>
            </setting>
            <setting name="Cur_Chute1_Lev" serializeAs="String">
                <value>TPS.KSS.Cur_Chute1_Lev</value>
            </setting>
            <setting name="Cur_Chute2_Lev" serializeAs="String">
                <value>TPS.KSS.Cur_Chute2_Lev</value>
            </setting>
            <setting name="Cur_Chute3_Lev" serializeAs="String">
                <value>TPS.KSS.Cur_Chute3_Lev</value>
            </setting>
            <setting name="PEPos1_2" serializeAs="String">
                <value>TPS.KSS.PEPos1_2</value>
            </setting>
            <setting name="PEPos2_2" serializeAs="String">
                <value>TPS.KSS.PEPos2_2</value>
            </setting>
            <setting name="PEPos3_2" serializeAs="String">
                <value>TPS.KSS.PEPos3_2</value>
            </setting>
            <setting name="ExitDoor" serializeAs="String">
                <value>TPS.KSS.ExitDoor</value>
            </setting>
            <setting name="EntryDoor" serializeAs="String">
                <value>TPS.KSS.EntryDoor</value>
            </setting>
            <setting name="PEExitDoor" serializeAs="String">
                <value>TPS.KSS.PEExitDoor</value>
            </setting>
            <setting name="PEEntryDoor" serializeAs="String">
                <value>TPS.KSS.PEEntryDoor</value>
            </setting>
            <setting name="PEShortTruck" serializeAs="String">
                <value>TPS.KSS.PEShortTruck</value>
            </setting>
        </MDCHartfordMVVM.Properties.Settings>
    </applicationSettings>
</configuration>

我想创建另一个可以加载和修改此文件的应用程序。我使用以下命令打开文件:

config = ConfigurationManager.OpenExeConfiguration(filepath);

其中 config 是配置对象,filepath 是我要修改的配置文件的文件路径。

这似乎可行,但是如果我设置断点并在分配配置后查看代码,则 appSettings 部分中没有任何设置。

有人知道为什么会这样吗?

这是正确的行为,因为您的配置中没有 appSettings 部分。您确实有一个 ApplicationSettings 部分。如果您更正拼写错误,它将起作用。

配置文件中的 appSettings 部分应该类似于

<appSettings>
    <add key="Setting1" value="some value"/>
    <add key="Setting2" value="some other value"/>
</appSettings>

'filepath' 变量应该是 .exe 的路径,而不是配置文件本身。

我获取了您的配置文件并将其作为 test.config 写入了我的临时文件夹。 您可以使用以下方法更改所需的设置:

var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = @"c:\temp\test.config" }, ConfigurationUserLevel.None);
var clientSettingsSection = (ClientSettingsSection)config.GetSection("applicationSettings/MDCHartfordMVVM.Properties.Settings");

clientSettingsSection.Settings.Get("EntryDoor").Value.ValueXml.ChildNodes[0].Value = "NewValue";

config.SaveAs(@"c:\temp\configuration.config",ConfigurationSaveMode.Modified, true);

有一些兴趣点。我使用 OpenMappedExeConfiguration 来获取使用 "applicationSettings/MDCHartfordMVVM.Properties.Settings" 的部分。这将在您的情况下得到 ClientSettingsSection 。使用此 ClientSettingsSection 我们可以访问列表中定义的设置。

我们可以通过更改底层 xml 来更改设置。我敢肯定还有另一种方法可以做到这一点,但我不想进一步研究它。

最后,我使用 ConfigurationSaveMode.Modified 和真实参数保存设置。这样我就可以在新配置文件中看到更改后的值。

但是一些默认的空元素也是使用它添加的。也许有人知道另一种方法。这就是我能弄清楚的。