4

I have two windows application. eg ., FormA and FormB

The app.config of FormA is as below

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <add key="company" value="DSRC"/>

    </appSettings>

<connectionStrings>

<add name="test" connectionString="Testing Connection String"/>

</connectionStrings>

</configuration>

Now I have another application named as Form B.

I want to retrieve both appsettings and connectionstrings of Form A into Form B.

Further I should be able to modify both of these appsettings and connection strings and save it into the Form A.

I know how to retrieve the appsettings , and connection strings of the same application and modify.

But how do I obtain of some other application and modify the same.

Kindly do let me know.

Actually I have 4 windows services running under one setup., one webservice and one wcf service and one application. All these have different app.configs, comprising of different appsettings and different connection strings. I am supposed to create a windows application that will retrieve each of these settings and then save it accordingly.

I tried upto this level

ExeConfigurationFileMap filename= new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"D:\Home\FormA\FormA\bin\Debug\FormA.exe.config";


Configuration config =
   ConfigurationManager.OpenMappedExeConfiguration(filename,
   ConfigurationUserLevel.None);

But then just got struck, I just do not know how to proceed further (Sounds dumb right !)

Can anyone help me proceed down the way.

Regards cmrhema

cmrhema
  • 981
  • 2
  • 16
  • 28

2 Answers2

2

Basically, you need to open the configuration for that other executable something like this:

// full path to other EXE, including EXE extension - but *NOT* .config !!
string otherExePath = @"C:\........\OtherApp\bin\Debug\OtherApp.exe";
Configuration otherConfig = 
              ConfigurationManager.OpenExeConfiguration(otherExePath);

and then you can access all the settings on the new otherConfig configuration:

string otherSetting = otherConfig.AppSettings.Settings["TestSetting1"].Value;

and you can also save it back (provided you have the necessary permissions to that directory).

otherConfig.Save():
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1
            ExeConfigurationFileMap fileMap2 
                = new ExeConfigurationFileMap();
            fileMap2.ExeConfigFilename = @"OtherFile";

            Configuration config2 =
              ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            ConnectionStringSettings newSettings = 
                config.ConnectionStrings.ConnectionStrings["oldSConString"];

            ConnectionStringsSection csSection 
                = config2.ConnectionStrings;

            csSection.ConnectionStrings.Add(newSettings);                
            config2.Save(ConfigurationSaveMode.Modified);

VS2005 C# Programmatically change connection string contained in app.config

Community
  • 1
  • 1
Asad
  • 21,468
  • 17
  • 69
  • 94