この空のプロパティを設定しました:
しかし、新しいArrayListオブジェクトをプロパティに割り当てた後でも、アプリケーションの実行ごとにプロパティは常に空のままです。
MyBase.Loadイベントのハンドラーで、この問題をテストするためだけに、このメソッドを呼び出します。
sub blahblah handles mybase.load
me.CheckRecentFiles
end sub
Private Sub CheckRecentFiles()
Try
' This throws an object not referenced exception 'cause always is empty.
MsgBox(My.Settings.RecentFiles.Count)
Catch ex As Exception
MsgBox(ex.Message)
End Try
' Just for testing, if the collection is empty then I instance a new ArrayList
' and I assign it to the property and I save it and exit from the application.
' But even doing this the property is always empty in the next execution.
If My.Settings.RecentFiles Is Nothing Then
My.Settings.RecentFiles = New ArrayList
My.Settings.RecentFiles.Add({"test-Item1", "test-Item2", "Test-Item3"})
My.Settings.Save()
Application.Exit()
End If
End Sub
上記のコードでわかるように、新しい ArrayList に 1 つのエントリを割り当てていますが、変更はそのアプリケーションの実行中にのみ有効になります。アプリを終了すると、プロパティは再び空になります。
そしてもちろん、私はこのオプションをチェックしました:
とにかく、設定を手動でコードに保存しているので、それは不要なので...
なぜこれが起こるのですか?
どうすればこの問題を解決できますか?.
アップデート:
私が調査したところ、これは既知の問題であり、Arrays、ArrayLists、および Generic collections(Of Type) を my.settings で保存できないようです (一方、StringCollection は保存できます)。
しかし、この投稿の最後の回答(MemoryStream の回答) では、ArrayList の変更を my.settings に永続的に保存し、次のアプリケーション実行でそれを読み取る簡単な方法について説明しています。
答えは非常に良いようですが、コードと続行する手順で少し迷っているので、そこで説明されている手順を誰かが説明できますが、人間が読める言語でお願いします。
ArrayList が次のアプリケーションの実行に残っていることを確認しました。はい、しかし、MemoryStream に古い ArrayList が含まれている場合、現在行っていることは My.Settings の割り当てです。 ? を含むArraylists
元の ArrayList の代わりに、より多くを含む Arraylist としての MRU 設定String()
、およびとにかく、この方法で設定を保存した後に配列エントリをロードする方法?.
これは私がその答えから試したことです:
' Create the ArrayList
Dim tmpArrayList = New System.Collections.ArrayList
tmpArrayList.Add({"test-Item1-1", "test-Item1-2", "Test-Item1-3"})
tmpArrayList.Add({"test-Item2-1", "test-Item2-2", "Test-Item2-3"})
' Serialize the arraylist entries:
Dim formatter As Runtime.Serialization.IFormatter =
New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim ms1 As New IO.MemoryStream
formatter.Serialize(ms1, tmpArrayList)
' Save the ArrayList
My.Settings.MRU = New ArrayList(ms1.ToArray) ' I think it hould be like that?
' Load the ArrayList contained in My.Settings.MRU (no idea)