IsolatedStorageFile Class in .NET


This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

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

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

Listing 22.34: IsolatedStorage Example 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;

namespace IsolatedStorageFile_Class
{
    class Program
    {
        static void Main(string[] args)
        {
            // isolated storage
            // in System.IO.IsolatedStorage namespace
            // Obtain a store
            IsolatedStorageFile store1 =
            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);
            */
            IsolatedStorageFileStream stream1 =
            new IsolatedStorageFileStream("file1.txt",
            FileMode.Create, store1);
            StreamWriter writer = new StreamWriter(stream1);
            writer.WriteLine("Hello Isolated Storage");
            writer.Close();
            stream1.Close();
            store1.Close();
            // Read from a file in isolated storage
            IsolatedStorageFileStream stream2 =
            new IsolatedStorageFileStream("file2.txt",
            FileMode.Open, store1);
            StreamReader reader = new StreamReader(stream1);
            String sb = 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
            IsolatedStorageFile store2 =
            IsolatedStorageFile.GetUserStoreForDomain();
            // Create an empty file
            IsolatedStorageFileStream stream3 =
            new IsolatedStorageFileStream("file3.txt",
            FileMode.Create, store1);
            stream1.Close();
            // Delete the file
            store1.DeleteFile("file3.txt");
            // Find a storage space available
            // Obtain a store
            IsolatedStorageFile store3 =
            IsolatedStorageFile.GetUserStoreForDomain();
            // Compute the storage available in the store
            ulong spaceLeft =
            store1.MaximumSize - store1.CurrentSize;
            // Enumerate files and directories
            // Obtain a store
            IsolatedStorageFile store4 =
            IsolatedStorageFile.GetUserStoreForDomain();
            // Get all of the directories in the root of the store
            string[] directories = store1.GetDirectoryNames("*");
            // Get all of the files in the root of the store
            string[] files = store1.GetFileNames("*");
        }
    }
}

Conclusion

Hope this article would have helped you in understanding IsolatedStorageFile Class in .NET. See other articles on the website on .NET and C#.

visual C-sharp.jpg
The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.


Similar Articles