看板 Visual_Basic 關於我們 聯絡資訊
我想請問一下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