You need to implement your section in App.config. There is some small console application, when you first start, the user enters a username and password, and they are sent to the config in their userinfo section, for example. The data will not change, I will not be added, it will only be necessary to get them from the config. Please offer a realization with at least some explanation of what, why, and why.

1 answer 1

App.config is a regular xml file and you can work with it as with an xml document using XDocument or XmlDocument. Any of these libraries will be able to read this file perfectly. The main thing - all changes in the file will take effect only after the application is restarted.

public void CustomAppConfig() { var path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; var config = XDocument.Load(path); var xElement = config.Element(XName.Get("configuration")); xElement?.Add(new XElement("myCustomSection", new XElement("User", "PasteUSerName"), new XElement("Password", "PasteUserPassword"))); config.Save("C:\\Your application path\\App.config"); } 

Reverse reading:

 public void CustomSectionRead() { var path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; var config = XDocument.Load(path); var xElement = config.Element(XName.Get("configuration")); if (xElement != null) { var myConf = xElement.Element(XName.Get("myCustomSection")); if (myConf != null) { foreach (var node in myConf.Elements()) { Console.WriteLine(node.Value); } } } } 
  • Minus is mine. Anxiety "is not the answer" is also mine. Pulls on the comment, no more. They asked for the same - an example of working with a section, they could paint it nicely and in detail and get well-deserved advantages. - AK
  • It is not clear how this answer is generally related to the custom section - tym32167
  • Please do not reply with the phrase "thank you". When your reputation reaches the required level, you will be able to vote for questions and answers that you find useful. - From the queue of checks - iluxa1810
  • @ iluxa1810 And where is the phrase "thank you"? - kot-da-vinci