Using isolated storage for application-specific data in C# .NET Part 3: storage location
August 28, 2015 Leave a comment
In this post we looked briefly at how to work with directories in isolated storage. In this post we’ll look at where isolated storage files are saved on disk depending on the isolation type: by user or by machine.
Recall our code to save the program settings specifically for the user:
private static void SaveSettingsInIsoStorage() { IsolatedStorageFile applicationStorageFileForUser = IsolatedStorageFile.GetUserStoreForAssembly(); long quotaInBytes = applicationStorageFileForUser.Quota; string[] directoryNames = applicationStorageFileForUser.GetDirectoryNames("AppSettings"); if (!directoryNames.Any()) { applicationStorageFileForUser.CreateDirectory("AppSettings"); } IsolatedStorageFileStream applicationStorageStreamForUser = new IsolatedStorageFileStream("AppSettings/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); } }
There’s no property that lets you easily read the actual location on disk. You’ll need to set a breakpoint right after this line:
private static void SaveSettingsInIsoStorage() { IsolatedStorageFile applicationStorageFileForUser = IsolatedStorageFile.GetUserStoreForAssembly(); ... }
When the code execution stops hover over “applicationStorageFileForUser” and open the “Non-public members” section. You’ll see a property called RootDirectory in the bottom of the list. The value should point to a user-specific location. In my case it is…
C:\Users\andras.nemes\AppData\Local\IsolatedStorage\nq213l1g.ul1\tflubnij.chu\Url.2ekkfnx05wuhzecg5x4y0q3tfifmqex4\AssemFiles\
…and there it is:
Let’s change the code so that we save the settings at the machine level:
private static void SaveSettingsInIsoStorage() { IsolatedStorageFile assemblyStorageFileForMachine = IsolatedStorageFile.GetMachineStoreForAssembly(); ... }
The same hidden RootDirectory property will point to the ProgramData folder similar to the following:
C:\ProgramData\IsolatedStorage\ofau1sef.fzp\lchabgk0.ywi\Url.2ekkfnx05wuhzecg5x4y0q3tfifmqex4\AssemFiles\
Let’s check:
Read the finishing part here.
Read all posts dedicated to file I/O here.