在VB.NET中保存结构数组的最佳方法是什么?
我有两个结构在VB.NET中保存结构数组的最佳方法是什么?,vb.net,arrays,file-io,structure,Vb.net,Arrays,File Io,Structure,我有两个结构 Public Structure One Public ItemOne As String Public ItemTwo As Integer End Structure Public Structure Two Public ItemOne As String Public ItemTwo As Integer
Public Structure One
Public ItemOne As String
Public ItemTwo As Integer
End Structure
Public Structure Two
Public ItemOne As String
Public ItemTwo As Integer
Public ItemThree As Integer
Public ItemFour As Integer
Public ItemFive As Integer
End Structure
Public TestOne(0) as One
Public TestTwo(19) as Two
使用FileOpen、FilePut和FileClose方法,我得到了一个错误:(作为一个示例,只涉及相关代码)
导致未处理的异常。坏记录长度。
然后,如果我关闭它并重新打开它,我会得到一个“无法读取超出流末尾的内容”错误
那么,保存结构数组的最佳方法是什么?二进制读写器?为什么这种方法不起作用(即使它是从VB6派生的)我认为保存结构数组的更好方法是使用序列化。您可以使用
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
,System.Xml.Serialization.XmlSerializer
或System.Runtime.Serialization.Formatters.Soap.SoapFormatter
对数组进行序列化。我认为保存结构数组的更好方法是使用序列化。您可以使用System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
、System.Xml.Serialization.XmlSerializer
或System.Runtime.Serialization.Formatters.Soap.SoapFormatter
对数组进行序列化。您可以使用序列化BinaryFormatter并将其保存到带有serialize的文件流中,然后使用反序列化读取它。您需要将
添加到结构声明中
<Serializable()> Public Structure Two
您可以使用serialization BinaryFormatter并使用Serialize将其保存到文件流中,然后使用Deserialize读取它。您需要将
添加到结构声明中
<Serializable()> Public Structure Two
哪一行导致了异常?啊,对不起,模块1[FilePut(1,TestTwo)]中的第34行,表单1[WriteOne()]中的第14行数组和读/写子程序位于另一个模块中。事件在form1中。哪一行导致异常?啊,对不起,module1[FilePut(1,TestTwo)]中的第34行,form1[WriteOne()]中的第14行数组和读/写子程序在另一个模块中。事件的格式为1。我将尝试一下并返回结果。谢谢也许我需要一点时间来找到如何使用它。我会尝试一下并返回结果。谢谢也许我需要一点时间才能找到如何使用它。
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim fStream As New FileStream(filename, FileMode.OpenOrCreate)
bf.Serialize(fStream, TestTwo) ' write to file
fStream.Position = 0 ' reset stream pointer
TestTwo = bf.Deserialize(fStream) ' read from file