我设置了这个空属性:

但是,在将一个新的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,但是这些更改只在应用程序执行期间生效,如果我退出应用程序,则属性再次变为空。
当然,我也检查了这个选项:

但无论如何,这是不必要的,因为我在代码中手动保存设置,所以.
为什么会发生这种事?
我怎样才能解决这个问题?
更新:
我已经调查过,而且似乎这是一个已知的问题,即数组、ArrayLists和任何泛型集合(类型)都不能被my.settings保存(但另一方面,StringCollection可以保存)
但是在this post的最后一个答案( MemoryStream答案)中,解释了一种简单的方法,可以将ArrayList的更改永久保存到my.settings,然后在下一个应用程序运行时读取它。
答案似乎很好,但我有点迷失在代码和步骤的加工,所以有人可以解释的步骤,但以人类可读的语言,请为我?
我已经验证了ArrayList仍然在下一次应用程序运行中,但我不确定我在做什么,因为如果MemoryStream包含旧的ArrayList,那么我现在所做的就是将My.Settings.MRU设置指定为包含更多Arraylists的Arraylist,而不是包含String()的原始ArrayList,而且无论如何,在以这种方式保存设置之后,如何加载数组条目?
这就是我从这个答案中尝试过的:
' 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)发布于 2014-07-29 11:42:50
如果您有arrayList (或List或Collection)中的数据,并且正在查看BinaryFormatter以解决这个问题,那么也没有充分的理由也使用My.Settings。您可以通过BinaryFormatter来完成它所做的事情,它只是保存文件并选择一个名称。
Imports System.Runtime.Serialization.Formatters.Binary
Private MRUArrayList = New ArrayList
' file location
myFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment. _
SpecialFolder.ApplicationData),
CompName,
ProgramName,
File)保存设置:
Dim bf As New BinaryFormatter
Using fs As New FileStream(myFile, FileMode.OpenOrCreate)
bf.Serialize(fs, MRUArrayList )
End Using负载设置:
' dont attempt for the first time run
If File.Exists(myFile) = False Then Return False
Dim bf As New BinaryFormatter
Using fs As New FileStream(myFile, FileMode.Open)
MRUArrayList = CType(bf.Deserialize(fs), ArrayList)
End Using一旦你不得不求助于BF来解决这个问题,用一个文件流代替内存流可以完全消除对My.Settings的需求,允许你在任何你想要的地方存储文件,它不会随版本而改变,除非你改变上面的文件名公式,否则用户会更改EXE名称或其他任何东西。
对于一个不仅仅是MRU ArrayList的应用程序,您可以使用一个类来将所有设置存储到您想要的位置,就像设置一样。只需将类标记为<Serializable>即可。它仍然是保存整个类的一行代码,还有一行用来重新构造它的代码。有一些限制,但并不难克服。
Private myNewSettings As New myNewSettingsClass
...
bf.Serialize(fs, myNewSettings)
myNewSettings = CType(bf.Deserialize(fs), myNewSettingsClass )在其他情况下,您可以根据需要使用XML序列化程序或ProtoBuf-NET。
您还可以在程序退出时自动保存新设置:转到项目属性--> Application -->单击。从左侧菜单中选择“应用程序事件”,从右侧事件菜单中选择ShutDown。
Private Sub MyApplication_Shutdown(sender As Object,
e As EventArgs) Handles Me.Shutdown
' add your code to Save (above) here
End Sub同样,您可以让它在Startup事件中自动加载它们。
https://stackoverflow.com/questions/25008539
复制相似问题