Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse isolated storage to save and restore settings in Visual Basic 2005
DescriptionThis example shows how to use isolated storage to save and restore settings in Visual Basic 2005.
Keywordsisolated storage, settings, save settings, Visual Basic, VB.NET
CategoriesSoftware Engineering
 
Visual Basic's SaveSetting and GetSetting methods let you easily save and restore settings in the Registry but that's not the only place you can store settings.

Isolated storage has the advantage that it is a purely .NET solution so it should work even on platforms that do not have a Registry, such as a Unix system using Mono to run a Visual Basic .NET application. (Although I haven't tried this because I don't have Unix installed. If you try it, let me know how it works.)

When the program starts, the following code loads the form's saved Left, Right, Width, and Height values. It creates an IsolatedStorageFile object representing the isolated storage file. It uses that to make an IsolatedStorageFileStream. It uses the parameter OpenOrCreate so an empty file is created if it doesn't already exist.

The code then associates a StreamReader with the stream and is ready to go. It uses Peek to see if the file is empty and, if it is not empty, it uses ReadLine to read the values from the file.

 
Private Const APP_NAME As String = _
    "howto_2005_isolated_storage"

' Reload the previously saved position and size.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Using storage_file As IsolatedStorageFile = _
     IsolatedStorageFile.GetUserStoreForAssembly
        Using file_stream As New IsolatedStorageFileStream( _
            _
         APP_NAME, FileMode.OpenOrCreate, storage_file)
            Using sr As New StreamReader(file_stream)
                ' Make sure the file contains something.
                If sr.Peek() >= 0 Then
                    Me.Left = sr.ReadLine()
                    Me.Top = sr.ReadLine()
                    Me.Width = sr.ReadLine()
                    Me.Height = sr.ReadLine()
                End If
            End Using
        End Using
    End Using
End Sub
 
When the application ends, the following code saves the form's settings. It makes an IsolatedStorageFile object, uses it to make a IsolatedStorageFileStream, associates a StreamWriter with it, and uses its WriteLine method to write into the file.
 
' Save position and size.
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.FormClosedEventArgs) Handles _
    Me.FormClosed
    Using storage_file As IsolatedStorageFile = _
     IsolatedStorageFile.GetUserStoreForAssembly
        Using file_stream As New IsolatedStorageFileStream( _
            _
         APP_NAME, FileMode.Create, storage_file)
            Using sw As New StreamWriter(file_stream)
                sw.WriteLine(Me.Left)
                sw.WriteLine(Me.Top)
                sw.WriteLine(Me.Width)
                sw.WriteLine(Me.Height)
            End Using
        End Using
    End Using
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated