Oak_3260548
Oak_3260548

Reputation: 2000

Saving Dictionary into My.Settings doesn't work

I am trying to save Dictionary(Of String, String) into My.Settings. It seems, that those settings only support System.Collection.Speciallized.StringDictionary, which is not exactly the same thing, but would be OK. I create a StringDictionary, fill it with a test data and try to save it, but it doesn't get saved. While another property CacheUpdateDate of type Date is saved fine, created or updated as needed. The scope of the properties is "user".

enter image description here

                Dim StrDict As New System.Collections.Specialized.StringDictionary
                For Each xmlf As KeyValuePair(Of String, String) In XMLfilesCache
                    StrDict.Add(xmlf.Key, xmlf.Value)
                Next
                Console.WriteLine("StrDict contains " & StrDict.Count.ToString & " files.")

                My.Settings.XMLcache = StrDict
                My.Settings.CacheUpdateDate = Date.Now
                My.Settings.Save()

Result:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <DocServiceMonitor.My.MySettings>
            <setting name="CacheUpdateDate" serializeAs="String">
                <value>05/15/2023 11:26:44</value>
            </setting>
            <setting name="XMLcache" serializeAs="Xml">
                <value />
            </setting>
        </DocServiceMonitor.My.MySettings>
    </userSettings>
</configuration>

Upvotes: 1

Views: 30

Answers (1)

Oak_3260548
Oak_3260548

Reputation: 2000

It appears there are several problems with using the StringDictionary in My.Settings and furthermore, I could see several advices against using this old type.

So my solution was to avoid My.Settings and simply save it to a dedicated text ("cache") file as an array of lines with | separator.

Upvotes: 0

Related Questions