ein Beispiel, das ich vor ca einem Jahr gepostet habe, und in diesem Forum immer wieder auftaucht, immer etwas abgewandelt, aber seltsamerweise immer ohne IsolatedStorage, auch wenn Namen wie "isfs" oft trotzdem übriggeblieben sind...
Public Class AppData
Public name As String
Public x As Integer
Public y As Integer
Public Sub New()
End Sub
End Class
Private _appData As AppData
Private _newConfig As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
LoadClientConfig()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As _
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
SaveClientConfig()
End Sub
' Load/save App settings
Private Sub LoadClientConfig()
Dim isf As IsolatedStorageFile
Dim isfs As IsolatedStorageFileStream
Dim sr As StreamReader
Try
isf = IsolatedStorageFile.GetUserStoreForAssembly()
isfs = New IsolatedStorageFileStream("AppData.xml", _
FileMode.OpenOrCreate, FileAccess.Read, isf)
If isfs.Length = 0 Then
' Init settings on first call for user
_appdata = New Appdata()
_appdata.name = "Unknown"
_appdata.x = 100
_appdata.y = 50
_newConfig = True
Else
sr = New StreamReader(isfs, System.Text.Encoding.UTF8)
Dim xmlser As New XmlSerializer(GetType(AppData))
_appData = CType(xmlser.Deserialize(sr), AppData)
End If
Catch ex As System.Exception
MsgBox("Error in reading settings: " + ex.Message, _
MsgBoxStyle.Exclamation, "Main")
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
If Not _appdata Is Nothing Then
' Set controls from settings
' ...
End If
End Sub
Public Sub SaveClientConfig()
If _appdata Is Nothing Then Exit Sub
Dim isf As IsolatedStorageFile
Dim isfs As IsolatedStorageFileStream
Dim sw As StreamWriter
Try
isf = IsolatedStorageFile.GetUserStoreForAssembly()
isfs = New IsolatedStorageFileStream("AppData.xml", _
FileMode.Create, FileAccess.Write, isf)
sw = New StreamWriter(isfs, System.Text.Encoding.UTF8)
' Save settings from controls
' ...
Dim xmlser As New XmlSerializer(GetType(AppData))
xmlser.Serialize(sw, _appData)
Catch ex As System.Exception
MsgBox("Could not save settings: " + ex.Message, _
MsgBoxStyle.Exclamation, "Main")
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
End Sub |