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.
source share