-1

I am trying to update the app.config file of another application from my project both are in c#. I know how to update it for the project in memory but not sure how to access the app.config file of my other project. I have following code but that will change the app.config file of my current project not the other one ....thanks for the suggestions or ideas

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

    foreach (XmlElement element in xmlDoc.DocumentElement)
    {
        if (element.Name.Equals("appSettings"))
        {
            foreach (XmlNode node in element.ChildNodes)
            {
                if (node.Attributes[0].Value.Equals("Setting1"))
                {
                    node.Attributes[1].Value = "New Value";
                }
            }
        }
    }
Prasanth V J
  • 1,126
  • 14
  • 32
J. Davidson
  • 3,297
  • 13
  • 54
  • 102
  • I haven't played with these classes, but I'm wondering if http://stackoverflow.com/questions/3912727/openmappedexeconfiguration-vs-openexeconfiguration could be of any use. Not a certainty, though. – Wim Ombelets Jan 09 '13 at 06:54

1 Answers1

1

Well, if the code shown works for the app.config file of the current project, it should work for the app.config file of the other project, you just need to load the file from the appropriate path. In other words, instead of doing:

 xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

You have to change it to:

 xmlDoc.Load("Path\\to\\config\\file\\of\\other\\application");

Or better yet, create a new method that takes the path to the app.config file as parameter and you can call it accordingly depending on which file you want to change....

Icarus
  • 63,293
  • 14
  • 100
  • 115