Isolated Storage with C# and .NET Framework

Computer System administrators put a lot of restrictions on accessing storage media such as disks with the proliferation of viruses and spyware around computer networks. It is now necessary to accommodate requirements to store data in a new way. As a result, isolated storage was implemented. Isolated storage allows storing data without resorting to checking for access rights and security privileges.

IsolatedStorageFile class provides functionality to create file and folders in isolated storage.

Static Methods

 

NameDescription
GetMachineStoreForApplication Gets a machine-level store for the Click-Once app.
GetMachineStoreForAssembly Gets a machine-level store for the assembly.
GetMachineStoreForDomain Gets a machine-level store for the AppDomain.
GetStore Gets stores based on the IsolatedStorageScope.
GetUserStoreForApplication Gets a user-level store for the Click-Once app.
GetUserStoreForAssembly Gets a user-level store for the assembly.
GetUserStoreForDomain Gets a user-level store for the AppDomain.

Properties

 

NameDescription
ApplicationIdentity The Click-Once application's identity.
AssemblyIdentity The assembly's identity.
CurrentSize The current size of the data in the storage.
DomainIdentity The identity of the AppDomain.
MaximumSize The maximum storage size.
Scope The IsolatedStorageScope enumeration value.

Methods

 

NameDescription
Close Closes an instance of a store.
CreateDirectory Creates a directory.
DeleteDirectory Deletes a directory.
DeleteFile Deletes a file.
GetDirectoryNames Gets a list of directory names.
GetFileNames Gets a list of file names.
Remove Removes the entire store.

In order to store data we need to determine first the scope of data application vs. user. As a result you are required to select one of the two methods: Assembly/User or Assembly/Machine. Creating stores is fairly simple.
IsolatedStorageFile machineStorage = IsolatedStorageFile.GetMachineStoreForAssembly();
IsolatedStorageFile userStorage = IsolatedStorageFile.GetUserStoreForAssembly();

IsolatedStorageFileStream class encapsulates a stream that is used to create files in such isolated storage. Since this class is derived from FileStream class it possesses the same properties and methods.

Reading or writing data to and from isolated storage is very similar to reading and writing from the file. We need to create file stream in this case it is “UserSetting.set”. Important to note that we don’t require to check for existing file since the way info is stored is totally random unlike with the files/folders on the physical disk. However, we can always find file by employing checking mechanism for the file name.
IsolatedStorageFile myUserStore = IsolatedStorageFile.GetUserStoreForAssembly();
IsolatedStorageFileStream myUserStream = new IsolatedStorageFileStream("UserSettingsFile.set", FileMode.Create, userStore);
//Check for file name
string[] files = myUserStore.GetFileNames("UserSettingsFile.set");
if (files.Length == 0)
    Console.WriteLine("No Saved Data");
Else
     ///

Isolated storage allows for the creation of directories and files being stored in those directories. You can also check for existence of directories the same way as for existence of the files inside isolated storage.
myUserStore.CreateDirectory("SomeDirOfMine");
IsolatedStorageFileStream myUserStream = new IsolatedStorageFileStream(@"SomeDirOfMine\UserSettings.set", FileMode.Create, userStore);
string[] directories = myUserStore.GetDirectoryNames("SomeDirOfMine");
if (directories.Length == 0)
    myUserStore.CreateDirectory("SomeDirOfMine");

In order to set permission to access isolated storage we use IsolatedStorageFilePermission class.

Properties

 

NameDescription
UsageAllowed Gets or sets the types.
UserQuota Gets or sets the overall size of storage.

[IsolatedStorageFilePermission(SecurityAction.Demand)]
class Program
{
    // ...
}