作者kucom (kucom)
看板Visual_Basic
标题[.NET] VB.NET序列化的问题
时间Tue Apr 14 17:33:51 2009
我想请问一下vb.net中的序列化的问题
我的需求是:
user在textbox1输入一组字串,按格式化(Button1)後序列化至c:\note\note.password
再按反格式化(button2)後,会反序列化还原原来的值至textbox2
我有写了,但我在按格式化後,用记事本去开note.password档,
发现档案中有完整的出现我刚刚输入的字串,及一些乱码
我是采二进位序列化格式的,照理说会分解成一些数字,不可能有原来的字串
想大家帮我看看,我的code,哪里出问题?
还是这样是正常的?
可否给我一些建议,谢谢!
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Public Class Form1
Dim password As String
Private Sub Button1_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles Button1.Click
Dim oFS As FileStream = Nothing
Dim oBF As BinaryFormatter = Nothing
If TextBox1.Text = "" Then
MsgBox("input dara plz!")
Else
password = TextBox1.Text
End If
Try
oFS = New FileStream("C:\note.password", FileMode.Create)
oBF = New BinaryFormatter
oBF.Serialize(oFS, password)
Catch ex As System.ArgumentNullException
MsgBox("错误!" & ex.Message.ToString)
Finally
If oFS IsNot Nothing Then
oFS.Close()
End If
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles Button2.Click
Dim oFS As FileStream = Nothing
Dim oBF As BinaryFormatter = Nothing
Try
oFS = New FileStream("C:\note.password", FileMode.Open)
oBF = New BinaryFormatter
password = oBF.Deserialize(oFS)
Catch ex As Exception
MsgBox("错误!" & ex.Message.ToString)
Exit Sub
Finally
If oFS IsNot Nothing Then
oFS.Close()
End If
End Try
TextBox2.Text = password
End Sub
End Class
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.96.98.77