1

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>
Oak_3260548
  • 1,882
  • 3
  • 23
  • 41
  • Problem as per [this answer](https://stackoverflow.com/a/6194818/7034621) is that `StringDictionary` does not implement `IXmlSerializable`. – orhtej2 May 15 '23 at 10:19
  • Thank you for the link. I missed that thread although I did quite in-depth search on SO and elsewhere. – Oak_3260548 May 15 '23 at 11:00

1 Answers1

0

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.

Oak_3260548
  • 1,882
  • 3
  • 23
  • 41