0

I have a .exe that points to two other .config files (NOT the .exe of the running .config). .config1 is the source and .config2 is the target. I want to copy a section that is present in .config1 into .config2. When I use this code:

targetConfig.Sections.Add("newSection", sourceSection);
targetConfig.Save(ConfigurationSaveMode.Modified);

I get an null ref, even though when I traverse the graph in both the source and the target, everything looks instantiated.

When I follow the MSDN docs about how to use the .Add method in context of thethe .OpenMappedExeConfig method found here, it creates a new class in code. When I do that, the insert works fine. Has anyone else run into this issue?

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Jamie Dixon
  • 4,204
  • 4
  • 25
  • 47
  • What line throws the exception? Is `targetConfig` null? `targetConfig.Sections`? – mjwills Mar 15 '18 at 12:02
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Camilo Terevinto Mar 15 '18 at 12:03

1 Answers1

0

I was incorrect, it was this exception Message = "Cannot add a ConfigurationSection that already belongs to the Configuration."

and this is the answer: https://social.msdn.microsoft.com/Forums/en-US/55727833-32e5-485a-ba8c-9c6db7e82e81/configurationadd-throws-exception?forum=vblanguage

Jamie Dixon
  • 4,204
  • 4
  • 25
  • 47