|
Tipp 0134
|
Binäre Serialisation
|
|
|
Autor/Einsender: Datum: |
|
Michael Werner 21.08.2006 |
|
Entwicklungsumgebung: |
|
VB.Net 2003 |
Framework: |
|
1.1 |
|
|
Sehr praktisch zum Abspeichern auch komplexer Objekte (z.B. Klassen, Strukturen, Arrays,
Collections) ist die Serialisation, hier am Beispiel
einer binären Serialisation einer Collection.
|
In diesem Tipp wird eine Hashtable-Collection erzeugt und binär gespeichert mittels BinaryFormatter
(Namespace System.Runtime.Serialization.Formatters.Binary) und schließlich wieder aus der gespeicherten Binärdatei ausgelesen.
|
|
|
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Dim hTable As New Collections.Hashtable
Dim dicEntry As DictionaryEntry
Dim sFilePath As String = Path.Combine( _
Application.UserAppDataPath, "SerialHash.dat")
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim stream As New FileStream(sFilePath, FileMode.Create, _
FileAccess.Write, FileShare.Write)
Dim converter As New BinaryFormatter
converter.Serialize(stream, hTable)
stream.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim stream As New FileStream(sFilePath, FileMode.Open, _
FileAccess.Read)
Dim converter As New BinaryFormatter
hTable = CType(converter.Deserialize(stream), Hashtable)
stream.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
ListAll()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = (hTable.Count + 1).ToString
If TextBox2.Text = String.Empty Then
MessageBox.Show("Geben Sie einen Wert ein!", _
"Keine Eingabe!", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Exit Sub
End If
hTable.Add(TextBox1.Text, TextBox2.Text)
ListAll()
End Sub
|
|
|
|
|
Windows-Version |
98/SE |
|
|
ME |
|
|
NT |
|
|
2000 |
|
|
XP |
|
|
Vista |
|
|
Win
7 |
|
|
|
|
Download (7,6 kB)
|
Downloads bisher: [ 480 ]
|
|
|