使用C#从* .config文件编辑UserSettings

时间:2015-06-06 16:42:37

标签: c# xml xmlnode

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="Tests" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <userSettings>
    <Tests>
      <setting name="var1" serializeAs="String">
        <value>value1</value>
      </setting>
      <setting name="var2" serializeAs="String">
        <value>value2</value>
      </setting>
      <setting name="var3" serializeAs="String">
        <value>value3</value>
      </setting> 
    </Tests>
  </userSettings>
</configuration>

有人可以帮我编写一段代码来动态编辑设置&#34; var2&#34;来自&#34; value2&#34;到&#34; abcd&#34;和&#34; var3&#34;来自&#34; value3&#34;到&#34; xyz&#34;并保存文件?

2 个答案:

答案 0 :(得分:1)

您可以使用ConfigurationManager更新配置文件。摘自MSDN:

 var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var settings = configFile.AppSettings.Settings;
            if (settings[key] == null)
            {
                settings.Add(key, value);
            }
            else
            {
                settings[key].Value = value;
            }
            configFile.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);

答案 1 :(得分:0)

我不知道自己做错了什么,最后这对我有用!!!抱歉打扰......

{代码}

        string settingsFilePath = Path.Combine(_asmblyFolderPathInHost, "InstallerTests.dll.config");

        XmlDocument installerConfig = new XmlDocument();
        installerConfig.Load(settingsFilePath);
        try
        {

            XmlElement machineName = (XmlElement)installerConfig.SelectSingleNode("//configuration/userSettings/InstallerTests.Resources.InstallernTests/setting[@name='MachineName']/value");
            if (machineName != null)
            {
                machineName.InnerText = machinename;
            }

            XmlElement vmName = (XmlElement)installerConfig.SelectSingleNode("//configuration/userSettings/InstallerTests.Resources.InstallernTests/setting[@name='VMName']/value");
            if (vmName != null)
            {
                vmName.InnerText = vmname;
            }

            XmlElement snapshotName = (XmlElement)installerConfig.SelectSingleNode("//configuration/userSettings/InstallerTests.Resources.InstallernTests/setting[@name='SnapshotName']/value");
            if (snapshotName != null)
            {
                snapshotName.InnerText = snapshotname;
            }

            installerConfig.Save(settingsFilePath);
        }
        catch (Exception e)
        {
            _consoleLogger.LogMessage(e.ToString());
        }       

{代码}