I am using the following code to write/edit the app.config file at runtime that allows the user to change ApplicationSettings however, I have a problem when writing pr saving null values.
Here is the code:
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim applicationSectionGroup As ConfigurationSectionGroup = config.GetSectionGroup("applicationSettings")
Dim strAppName As String = System.Reflection.Assembly.GetExecutingAssembly.GetName().Name
Dim applicationConfigSection As ConfigurationSection = applicationSectionGroup.Sections(strAppName & ".My.MySettings")
Dim clientSection As ClientSettingsSection = CType(applicationConfigSection, ClientSettingsSection)
' set a value to that specific property
Dim applicationSetting As SettingElement
applicationSetting = clientSection.Settings.Get("Testsetting")
applicationSetting.Value.ValueXml.InnerText = Nothing
' without this, saving won't work
applicationConfigSection.SectionInformation.ForceSave = True
' save
config.Save()
The problem is, when I save null values with this code, the Value tag is not written correctly. It's writing the tag as follows:
The problem is, when I read the tag using My.Settings.Testsetting, it's being read by vb.net as multiple spaces instead of a null or empty string. I know I can use a trim statement to read the settings and clear the spaces but, I would rather find a way to save the empty value correctly instead of working around the problem.
In my production code, I am passing the Value in as a string but for testing purposes above, I have been trying to find a way to save it as a self-closing value like
<value />
That's how visual studio saves the tag with a null value when saved from the Project Property Settings screen.
Any help would be really appreciated.
Thanks