vb.net - How can I write to my own app.config using a strongly typed object? -


The following code has two flaws, I do not know if they are bugs or what I have seen with the design, It is possible to write back to the app.config file using. And work according to the code.

The bugs are shown in the source given below and then appear when you return to the property config or try to save

  Import System.Configuration Public Class ConfigTest Legacy Found in ConfigurationSection & LT; ConfigurationProperty ("JunkProperty", IsRequired: = this is true). & Gt; _Public property JunkProperty (return) string as CSTR (get me ("JunkProperty")) Get Set End Go (as byVal Value String) '*** Bug 1, Configuration ConfigurationErrorsException with Message Configuration' only read Is for." Throw me on the following line ("JunkProperty") = Set End End Set End Property Public Sub Save () as 'Configure ConfigManager Configuration = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None)' Add / Remove http://www.codeproject.com According to / KB / Cs / SystemConfiguration.aspx ConfigManager.Sections.Remove ("ConfigTest") '*** Bugs thrown with the exception invalid OperationException message' can not add a ConfigurationSection that is already under the configuration Are. " ConfigManager.Sections.Add ( "ConfigTest", I) 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" should be AppConfig.Save () and sub 'App.Config:' & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; '& Lt; Configuration & gt; '& Lt; ConfigSections & gt; '& Lt; Section name = "ConfigTest" type = "ConsoleApp.ConfigTest, ConsoleApp" /> '& Lt; / ConfigSections & gt; '& Lt; ConfigTest JunkProperty = "" /> '& Lt; / Configuration & gt; And Class  

I want to do this in such a way that I check the properties on the first part of the app and then ask the user to run as admin, if they should be set , Where UI will help them with settings. I do not already have any effect on 'running as an administrator'.

Your code really does not understand anyone does. I took your example code and turned it into a simple example, which works. Please note that this is not the best practice code, only one example configuration API to assist on your journey of learning

  PublicTrust ConfigTest inherited ConfigurationSection & LT; ConfigurationProperty ("JunkProperty", IsRequired:. = True)> _Public property JunkProperty (return) string as CSTR (get me ("JunkProperty")) Get Set End Go (as byVal Value String) '*** Bug 1, Configuration ConfigurationErrorsException with Message Configuration' only read Is for." I dropped the following line ("Junkproperty") = Value & Set End Property Public Override Function IsReadOnly () Return Boolean as Wrong End Function Public Shared Sub Main () Dim configuration as Configuration = Configuration Manager. OpenExeConfiguration (ConfigurationUserLevel.None) Dim AppConfig ConfigTest = config as GetSection ( "ConfigTest") AppConfig.JunkProperty = "some test data" config.Save () End Sub End Class  

This To open the code config file, modify the attribute JunkProperty and continue it back to the executable file that is executable, hopefully it will start you - it looks like you need to read the configuration API slightly more.

I have extensively used the API with several other 1000 lines to create a configuration section for enterprise apps custom hierarchical config (my config was read-only) Configuration API has many powerful when you Have learned it. I had to use the reflector in a way to know about its capabilities to see how the NIT framework internally uses the API.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -