Use Isolated Storage for Configuration Data

When storing user-specific program settings, I like using the Isolated File Storage capability of Windows and .NET.  This allows a program to place files into a location that is specific to that user and assembly.  Here's how:

Saving:
Dim isoFile As IsolatedStorageFile
Dim isoStream As IsolatedStorageFileStream
Dim bf As BinaryFormatter
Dim optMap As Hashtable
 
optMap = New Hashtable()
optMap.Add(“Key1”, 12)
optMap.Add(“Key2”, 20)
 
isoFile = IsolatedStorageFile.GetUserStoreForAssembly
isoStream = New IsolatedStorageFileStream(“MyApp.cfg”,_ IO.FileMode.Create, isoFile)
bf = New BinaryFormatter()
bf.Serialize(isoStream,optMap)

Loading:
Dim isoFile As IsolatedStorageFile
Dim isoStream As IsolatedStorageFileStream
Dim bf As BinaryFormatter
Dim optMap As Hashtable
 
isoFile = IsolatedStorageFile.GetUserStoreForAssembly
isoStream = New IsolatedStorageFileStream_(FILENAME,IO.FileMode.OpenOrCreate, isoFile)
 
If isoStream.Length > 0 Then
bf = New BinaryFormatter()
      optMap = CType(bf.Deserialize(isoStream),Hashtable)
 
      x = CType(optMap(“Key1”),integer)
      y = CType(optMap(“Key2”),integer)
EndIf

Comments