0
votes

In a nutshell, I'm trying to modify another application's configuration file. i have 2 apps, App A and AppB. App A needs to change the config file of App B. How do i go by updating the applicationSettings section within that config file shown below?

   <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <applicationSettings>
            <Sample.Settings1>
                <setting name="DBUser" serializeAs="String">
                  <value>sa</value>
                </setting>
                <setting name="DBPass" serializeAs="String">
                  <value>Sample12345</value>
                </setting>
                <setting name="DBServer" serializeAs="String">
                  <value>.\SQL2017</value>
                </setting>
                <setting name="DBCatalog" serializeAs="String">
                  <value>SMPL</value>
                </setting>
            </Sample.Settings1>
        </applicationSettings>
    <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup></configuration>

This is how it adds it in when i run Application A. But Application B uses the applicationSettings value and not the AppSettings value.

enter code here
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="server" value=".\SQL2017" />
        <add key="catalog" value="SMPL" />
        <add key="UserID" value="sa" />
        <add key="Pwd" value="Sample12345" />
    </appSettings>
    <applicationSettings>
        <Sample.Settings1>
            <setting name="DBUser" serializeAs="String">
              <value>sa</value>
            </setting>
            <setting name="DBPass" serializeAs="String">
              <value>Sample12345</value>
            </setting>
            <setting name="DBServer" serializeAs="String">
              <value>.\SQL2017</value>
            </setting>
            <setting name="DBCatalog" serializeAs="String">
              <value>SMPL</value>
            </setting>
        </Sample.Settings1>
    </applicationSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup></configuration>
1
Are they built together, or are you just trying to modify an arbitrary application's config at run time?Aluan Haddad
@AluanHaddad, option 2. I'm trying to modify an arbitrary application's config at run timeGolfbuddy85

1 Answers

1
votes

I think this is a very simple example of what you're seeking.

    var pathToConfig = @"C:\somepath";
// Using ExeConfigurationFileMap so you can grab the app.config directly.
// Load the config based on the path
  var configMap = new ExeConfigurationFileMap { ExeConfigFilename = pathToConfig };
  var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    
    //Make whatever changes you want in code here
    var s = config.Sections.Get("applicationSettings");
    // cast s to whatever section type you want, make changes, and then save.

    // Save it, either back to where you found it, or somewhere else
    config.SaveAs(pathToConfig);

Alternatively, you could just load it as XML, manipulate it, and then write it back out.