Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I write to my own app.config using a strongly typed object?
    primarykey
    data
    text
    <p>The following code has two flaws, I can't figure out if they are bugs or by design. From what I have seen it should be possible to write back to the app.config file using the Configuration.Save and according to <a href="http://www.codeproject.com/KB/cs/SystemConfiguration.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/cs/SystemConfiguration.aspx</a> the code should work.</p> <p>The bugs are shown in the source below and appear when you try to set the property or save the config back out.</p> <pre><code>Imports System.Configuration Public Class ConfigTest Inherits ConfigurationSection &lt;ConfigurationProperty("JunkProperty", IsRequired:=True)&gt; _ 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: ' &lt;?xml version="1.0" encoding="utf-8" ?&gt; '&lt;configuration&gt; ' &lt;configSections&gt; ' &lt;section name="ConfigTest" type="ConsoleApp.ConfigTest, ConsoleApp" /&gt; ' &lt;/configSections&gt; ' &lt;ConfigTest JunkProperty="" /&gt; '&lt;/configuration&gt; End Class </code></pre> <p>I'd like to do it this way so that on the first run of the app I check for the properties and then tell the user to run as admin if they need to be set, where the UI would help them with the settings. I've already 'run as admin' to no effect.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload