Ja, klar, man kann alls speichern. In so einem Fall schreibe ich die notwendigen Daten in ein Klassenobjekt, und das wird mit Xml Serialisierung geschrieben und wieder gelesen. Hier ist eine Klasse für die Serialisierung
Imports System.IO
Imports System.IO.IsolatedStorage
Imports System.Xml
Imports System.Xml.Serialization
Public Class AppDataManager
' Load/save App settings to local file
Public Shared Function LoadClientConfig(ByVal initData As Object, ByVal _
file As String) As Object
Dim fs As FileStream
Dim sr As StreamReader
Try
fs = New FileStream(file, FileMode.OpenOrCreate, FileAccess.Read)
If fs.Length = 0 Then
Return initData
Else
sr = New StreamReader(fs, System.Text.Encoding.UTF8)
Dim xmlser As New XmlSerializer(initData.GetType())
Return xmlser.Deserialize(sr)
End If
Catch ex As System.Exception
MsgBox("Error in reading settings: " + ex.Message, _
MsgBoxStyle.Exclamation, "Main")
Return initData
Finally
If Not sr Is Nothing Then sr.Close()
If Not fs Is Nothing Then fs.Close()
End Try
End Function
Public Shared Function SaveClientConfig(ByVal data As Object, ByVal file As _
String) As Boolean
If data Is Nothing Then Exit Function
Dim fs As FileStream
Dim sw As StreamWriter
Try
fs = New FileStream(file, FileMode.Create, FileAccess.Write)
sw = New StreamWriter(fs, System.Text.Encoding.UTF8)
Dim xmlser As New XmlSerializer(data.GetType())
xmlser.Serialize(sw, data)
Catch ex As System.Exception
MsgBox("Could not save settings: " + ex.Message, _
MsgBoxStyle.Exclamation, "Main")
Return False
Finally
If Not sw Is Nothing Then sw.Close()
If Not fs Is Nothing Then fs.Close()
End Try
Return True
End Function
' Load/save App settings to Isolated Storage
Public Shared Function LoadClientConfigIS(ByVal initData As Object, ByVal _
file As String) As Object
Dim isf As IsolatedStorageFile
Dim isfs As IsolatedStorageFileStream
Dim sr As StreamReader
Try
isf = IsolatedStorageFile.GetUserStoreForAssembly()
isfs = New IsolatedStorageFileStream(file, FileMode.OpenOrCreate, _
FileAccess.Read, isf)
If isfs.Length = 0 Then
Return initData
Else
sr = New StreamReader(isfs, System.Text.Encoding.UTF8)
Dim xmlser As New XmlSerializer(initData.GetType())
Return xmlser.Deserialize(sr)
End If
Catch ex As System.Exception
MsgBox("Error in reading settings: " + ex.Message, _
MsgBoxStyle.Exclamation, "Main")
Return initData
Finally
If Not sr Is Nothing Then sr.Close()
If Not isfs Is Nothing Then isfs.Close()
If Not isf Is Nothing Then isf.Close()
End Try
End Function
Public Shared Function SaveClientConfigIS(ByVal data As Object, ByVal file _
As String) As Boolean
If data Is Nothing Then Exit Function
Dim isf As IsolatedStorageFile
Dim isfs As IsolatedStorageFileStream
Dim sw As StreamWriter
Try
isf = IsolatedStorageFile.GetUserStoreForAssembly()
isfs = New IsolatedStorageFileStream(file, FileMode.Create, _
FileAccess.Write, isf)
sw = New StreamWriter(isfs, System.Text.Encoding.UTF8)
Dim xmlser As New XmlSerializer(data.GetType())
xmlser.Serialize(sw, data)
Catch ex As System.Exception
MsgBox("Could not save settings: " + ex.Message, _
MsgBoxStyle.Exclamation, "Main")
Return False
Finally
If Not sw Is Nothing Then sw.Close()
If Not isfs Is Nothing Then isfs.Close()
If Not isf Is Nothing Then isf.Close()
End Try
Return True
End Function
End Class (Die ..IS Routinen benutzen IsolatedStorage).
Das wird dann so verwendet:
' Klasse für die Anwendungsdaten
Public Class AppData
Public url As String = "[no url]"
Public user As String = "[no user]"
Public password As String = "[no password]"
End Class
Private configData As AppData
Private _newConfig As Boolean
Private file As String = "AppConfig.xml"
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
' Read the config data
Dim nad As New AppData
configData = AppDataManager.LoadClientConfig(nad, file)
_newConfig = configData Is nad
' Set app values from config data
TextBox1.Text = configData.url
TextBox2.Text = configData.user
TextBox3.Text = configData.password
End Sub
Protected Overrides Sub OnClosing(ByVal e As _
System.ComponentModel.CancelEventArgs)
' Set new config data values
configData.url = TextBox1.Text
configData.user = TextBox2.Text
configData.password = TextBox3.Text
' Write config data
AppDataManager.SaveClientConfig(configData, file)
End Sub
End Class ________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |