首先建立一个MyObject类,代码如下:
但后就是调用存储和读取的代码:
最后的界面如下
1
2
<Serializable()>Public Class MyObject
3
Public n1 As Integer = 0
4
Public n2 As Integer = 0
5
Public str As String = Nothing
6
End Class
7
注意如果是c#则在类前添加[Serializable]代码如下:
2
<Serializable()>Public Class MyObject3
Public n1 As Integer = 04
Public n2 As Integer = 05
Public str As String = Nothing6
End Class7

1
[Serializable]
2
public class MyObject {
3
public int n1 = 0;
4
public int n2 = 0;
5
public String str = null;
6
}
7
[Serializable]2
public class MyObject {3
public int n1 = 0;4
public int n2 = 0;5
public String str = null;6
}7

但后就是调用存储和读取的代码:
1
Imports System.Runtime.Serialization
2
Imports System.Data
3
Imports System.Diagnostics
4
Imports System.Runtime.Serialization.Formatters.Binary
5
Imports System.IO
6
7
8
Public Class mainForm
9
Inherits System.Windows.Forms.Form
10
11
Windows 窗体设计器生成的代码
165
166
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
167
168
End Sub
169
170
Private Sub txtWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWrite.Click
171
172
Dim obj As New MyObject
173
174
If IsNumeric(Me.txtN1.Text.Trim()) Then
175
obj.n1 = CType(Me.txtN1.Text.Trim, System.Int32)
176
Else
177
obj.n1 = 0
178
End If
179
If IsNumeric(Me.txtN2.Text.Trim()) Then
180
obj.n2 = CType(Me.txtN2.Text.Trim, System.Int32)
181
Else
182
obj.n2 = 0
183
End If
184
185
obj.str = Me.txtStr.Text.Trim
186
Dim oFormatter As IFormatter = New BinaryFormatter
187
Dim oStream As Stream = New FileStream("mingfei.dat", FileMode.Create, FileAccess.Write, FileShare.None)
188
oFormatter.Serialize(oStream, obj)
189
oStream.Close()
190
MessageBox.Show("序列化存储完成!")
191
192
End Sub
193
194
Private Sub txtRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRead.Click
195
Dim oFormatter As IFormatter = New BinaryFormatter
196
Dim oStream As Stream = New FileStream("mingfei.dat", FileMode.Open, FileAccess.Read, FileShare.Read)
197
Dim obs As New MyObject
198
obs = CType(oFormatter.Deserialize(oStream), MyObject)
199
oStream.Close()
200
Me.lblN1.Text = obs.n1.ToString()
201
Me.lblN2.Text = obs.n2.ToString
202
Me.lblStr.Text = obs.str
203
MessageBox.Show("序列化读取完成!")
204
End Sub
205
End Class
206
Imports System.Runtime.Serialization2
Imports System.Data3
Imports System.Diagnostics4
Imports System.Runtime.Serialization.Formatters.Binary5
Imports System.IO6

7

8
Public Class mainForm9
Inherits System.Windows.Forms.Form10

11
Windows 窗体设计器生成的代码165

166
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load167

168
End Sub169

170
Private Sub txtWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWrite.Click171

172
Dim obj As New MyObject173

174
If IsNumeric(Me.txtN1.Text.Trim()) Then175
obj.n1 = CType(Me.txtN1.Text.Trim, System.Int32)176
Else177
obj.n1 = 0178
End If179
If IsNumeric(Me.txtN2.Text.Trim()) Then180
obj.n2 = CType(Me.txtN2.Text.Trim, System.Int32)181
Else182
obj.n2 = 0183
End If184

185
obj.str = Me.txtStr.Text.Trim186
Dim oFormatter As IFormatter = New BinaryFormatter187
Dim oStream As Stream = New FileStream("mingfei.dat", FileMode.Create, FileAccess.Write, FileShare.None)188
oFormatter.Serialize(oStream, obj)189
oStream.Close()190
MessageBox.Show("序列化存储完成!")191

192
End Sub193

194
Private Sub txtRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRead.Click195
Dim oFormatter As IFormatter = New BinaryFormatter196
Dim oStream As Stream = New FileStream("mingfei.dat", FileMode.Open, FileAccess.Read, FileShare.Read)197
Dim obs As New MyObject198
obs = CType(oFormatter.Deserialize(oStream), MyObject)199
oStream.Close()200
Me.lblN1.Text = obs.n1.ToString()201
Me.lblN2.Text = obs.n2.ToString202
Me.lblStr.Text = obs.str203
MessageBox.Show("序列化读取完成!")204
End Sub205
End Class206

最后的界面如下


浙公网安备 33010602011771号