Using isolated storage for application-specific data in C# .NET Part 1: the basics

There’s a special storage location for a .NET application on Windows that is allocated to that application. It’s called isolated storage and it’s an optimal place to store files by an application that doesn’t have full access to the file system. Writing to and reading from isolated storage doesn’t require any extra security check. An application without full access to the file system will be able to use its allocated slot in isolated storage and nothing else. It’s an ideal mechanism for storing e.g. application state.

However, don’t confuse isolated storage with file security. It is still a “normal” location on disk with a file path. A typical location is under users/[username]/appdata/local. Therefore you or full-trust applications can still find and modify the files saved in isolated storage. However, limited-trust applications won’t be able to access any other part of the file system.

The two key objects into isolated storage are IsolatedStorageFile and IsolatedStorageFileStream. Let’s see how a custom AppSettings object can be saved for a user in isolated storage:

private static void SaveSettingsInIsoStorage()
{
	IsolatedStorageFile applicationStorageFileForUser = IsolatedStorageFile.GetUserStoreForAssembly();
	IsolatedStorageFileStream applicationStorageStreamForUser = new IsolatedStorageFileStream("settings.txt", FileMode.Create, applicationStorageFileForUser);
	AppSettings settings = new AppSettings()
	{
		Job = "Programmer",
		Language = "C#",
		Name = "Andras"
	};
	string contents = JsonConvert.SerializeObject(settings);

	using (StreamWriter sw = new StreamWriter(applicationStorageStreamForUser))
	{
		sw.WriteLine(contents);
	}
}

…where AppSettings looks like this:

public class AppSettings
{
	public string Language { get; set; }
	public string Name { get; set; }
	public string Job { get; set; }
}

We simply save the settings object as a JSON string in the assembly-specific section of the isolated storage at the user level. We can also store machine-wide settings for an application using the GetMachineStoreForAssembly method instead.

Reading from isolated is equally easy:

private static void ReadSettingsFromIsoStorage()
{
	IsolatedStorageFile applicationStorageFileForUser = IsolatedStorageFile.GetUserStoreForAssembly();
	IsolatedStorageFileStream applicationStorageStreamForUser = new IsolatedStorageFileStream("settings.txt", FileMode.Open, applicationStorageFileForUser);
	using (StreamReader sr = new StreamReader(applicationStorageStreamForUser))
	{
		string raw = sr.ReadLine();
		Debug.WriteLine(raw);
	}
}

Here’s the output:

{“Language”:”C#”,”Name”:”Andras”,”Job”:”Programmer”}

Read the next part here.

Read all posts dedicated to file I/O here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

3 Responses to Using isolated storage for application-specific data in C# .NET Part 1: the basics

  1. ayesha says:

    What if we need to read the isolated storage file in an external application written by another application? how will we determine the correct path?

    • Andras Nemes says:

      I don’t think that’s possible. Each application has its own its own section of isolated storage, they are not meant to be shared across independent processes.

  2. P.Norman says:

    Let’s assume I have different versions of the same application.
    How to manage that each version has it’s own isolated storage when strongname is the same?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: