How can I write my own app.config using a strongly typed object?

The following code has two drawbacks, I cannot understand if they are errors or by design. From what I saw, it should be possible to write back to the app.config file using Configuration.Save and in accordance with http://www.codeproject.com/KB/cs/SystemConfiguration.aspx the code should work.

Errors appear in the source below and appear when you try to set a property or save a configuration.

Imports System.Configuration Public Class ConfigTest Inherits ConfigurationSection <ConfigurationProperty("JunkProperty", IsRequired:=True)> _ Public Property JunkProperty() As String Get Return CStr(Me("JunkProperty")) End Get Set(ByVal value As String) ' *** Bug 1, exception ConfigurationErrorsException with message "The configuration is read only." thrown on the following line. Me("JunkProperty") = value End Set End Property Public Sub Save() Dim ConfigManager As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) ' The add / remove is according to http://www.codeproject.com/KB/cs/SystemConfiguration.aspx ConfigManager.Sections.Remove("ConfigTest") ' *** Bug 2, exception InvalidOperationException thrown with message "Cannot add a ConfigurationSection that already belongs to the Configuration." ConfigManager.Sections.Add("ConfigTest", Me) ConfigManager.Save(ConfigurationSaveMode.Full, True) End Sub Public Shared Sub Main() Dim AppConfig As ConfigTest = TryCast(ConfigurationManager.GetSection("ConfigTest"), ConfigTest) AppConfig.JunkProperty = "Some test data" AppConfig.Save() End Sub ' App.Config should be: ' <?xml version="1.0" encoding="utf-8" ?> '<configuration> ' <configSections> ' <section name="ConfigTest" type="ConsoleApp.ConfigTest, ConsoleApp" /> ' </configSections> ' <ConfigTest JunkProperty="" /> '</configuration> End Class 

I would like to make it so that the first time I start the application, I check the properties and then tell the user to run as admin if they need to install where the user interface will help them with the settings. I already "run as administrator" without effect.

+4
source share
5 answers

Your code doesn't really make any sense. I took your sample code and turned it into a simple example that works. Please note that this is not the best code, just an example that will help you learn the configuration API.

 Public Class ConfigTest Inherits ConfigurationSection <ConfigurationProperty("JunkProperty", IsRequired:=True)> _ Public Property JunkProperty() As String Get Return CStr(Me("JunkProperty")) End Get Set(ByVal value As String) ' *** Bug 1, exception ConfigurationErrorsException with message "The configuration is read only." thrown on the following line. Me("JunkProperty") = value End Set End Property Public Overrides Function IsReadOnly() As Boolean Return False End Function Public Shared Sub Main() Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) Dim AppConfig As ConfigTest = config.GetSection("ConfigTest") AppConfig.JunkProperty = "Some test data" config.Save() End Sub End Class 

This code will open the configuration file, change the JunkProperty attribute and save its executable configuration file. Hope this helps you get started - it looks like you need to learn a little about the configuration API.

I used the API to create configuration sections for large enterprise applications with several thousand lines of custom hierarchical configuration (my configuration was read-only). The configuration API is very powerful once you recognize it. One way to learn more about its features is to use Reflector to see how the .NET environment uses the API internally.

+1
source

You may not know Portuguese or C #, but you want it http://www.linhadecodigo.com.br/Artigo.aspx?id=1613

using BuildProvider from asp.net

0
source

After loading the configuration, it is used by default by default, mainly because you did not override the IsReadOnly property. Try to override it.

ΒΏIs there anything that prevents you from using the settings?

0
source

It seems like it's impossible by design. App.config is usually protected because it is located with the application in the Program Files directory, so the installer must make changes during installation.

In fact, it is a pity that the application should have settings that the administrator can set.

0
source

Sorry if I did not understand your case, but yes, you can change App.config at runtime.

In fact, you will need to modify the YourApp.exe.config file, because as soon as your application is compiled, the contents of the App.config will be copied to the YourApp.exe.config file and your application will never return to App.config.

So here is what I am doing (C # code - sorry, I still haven't recognized VB.Net)

  public void UpdateAppSettings(string key, string value) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); foreach (XmlElement item in xmlDoc.DocumentElement) { foreach (XmlNode node in item.ChildNodes) { if (node.Name == key) { node.Attributes[0].Value = value; break; } } } using (StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)) { xmlDoc.Save(sw); } 
0
source

Source: https://habr.com/ru/post/1286757/


All Articles