VB.NET IsolatedStorageFile Class

In this article I will explain you about IsolatedStorageFile Class in VB.NET.
  • 2139

The .NET Framework provides the IsolatedStorageFile class to represent a store for an assembly.

Listing A shows various aspects of IsolatedStorage namespace classes and methods.

Listing A: IsolatedStorage Example

Imports
System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO.IsolatedStorage
Imports System.IO 
Namespace IsolatedStorageFile_Class
    Class Program
        Shared Sub Main(ByVal args As String())
            ' isolated storage
            ' in System.IO.IsolatedStorage namespace
            ' Obtain a store

            Dim store1 As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain()
            ' you can also use static GetStore methods like below
            '
            '            IsolatedStorageFile storefile1 = IsolatedStorageFile.GetStore(
            '            IsolatedStorageScope.User |
            '            IsolatedStorageScope.Assembly,
            '            null,
            '            null)
            '            IsolatedStorageFile storefile1 = IsolatedStorageFile.GetStore(
            '            IsolatedStorageScope.User |
            '            IsolatedStorageScope.Assembly |
            '            IsolatedStorageScope.Domain,
            '            null,
            '            null)
            '            IsolatedStorageFile storefile1 = IsolatedStorageFile.GetStore(
            '            IsolatedStorageScope.User |
            '            IsolatedStorageScope.Assembly |
            '            IsolatedStorageScope.Roaming,
            '            null,
            '            null)
            '            IsolatedStorageFile storefile1 = IsolatedStorageFile.GetStore(
            '            IsolatedStorageScope.User |
            '             * IsolatedStorageScope.Assembly |
            '            IsolatedStorageScope.Domain |
            '            IsolatedStorageScope.Roaming,
            '            null,
            '            null)
            '           
            Dim stream1 As New IsolatedStorageFileStream("file1.txt", FileMode.Create, store1)
            Dim writer As New StreamWriter(stream1)
            writer.WriteLine("Hello Isolated Storage")
            writer.Close()
            stream1.Close()
            store1.Close()

            ' Read from a file in isolated storage
            Dim stream2 As New IsolatedStorageFileStream("file2.txt", FileMode.Open, store1)
            Dim reader As New StreamReader(stream1)
            Dim sb As [String] = reader.ReadToEnd()
            reader.Close()
            stream1.Close()
            store1.Close()

            ' Obtain a store
            ' Create a directory
            store1.CreateDirectory("dir1")

            ' Create two directories, one inside the other
            store1.CreateDirectory("dir2/dir3")

            ' Create a directory
            store1.CreateDirectory("dir4")

            ' Delete the directory
            store1.DeleteDirectory("dir4")

            ' Obtain a store
            Dim store2 As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain()

            ' Create an empty file
            Dim stream3 As New IsolatedStorageFileStream("file3.txt", FileMode.Create, store1)
            stream1.Close()

            ' Delete the file
            store1.DeleteFile("file3.txt")

            ' Find a storage space available
            ' Obtain a store
            Dim store3 As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain()

            ' Compute the storage available in the store
            Dim spaceLeft As ULong = store1.MaximumSize - store1.CurrentSize

            ' Enumerate files and directories
            ' Obtain a store
            Dim store4 As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain()

            ' Get all of the directories in the root of the store
            Dim directories As String() = store1.GetDirectoryNames("*")

            ' Get all of the files in the root of the store
            Dim files As String() = store1.GetFileNames("*")
        End Sub
    End Class
End Namespace

Conclusion

Hope this article would have helped you in understanding IsolatedStorageFile Class in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.