What is the Isolated Storage Location in Windows 7 and 8?

Shafiq Abbas picture Shafiq Abbas · May 22, 2013 · Viewed 7.5k times · Source

I want to store a file in my Windows 8 or Windows 7 Isolated Storage location.? Knowing the location, i want to place some files in the location. So, how to get the location of the isolated storage file loction..?

 using(IsolatedStorageFile isoPath=IsolatedStorageFile.GetUserStoreForApplication())
{
}

What is the location we get on IsolatedStorageFile.GetUserStoreForApplication()...?

Answer

DmitryBoyko picture DmitryBoyko · Dec 9, 2013

Here you can find complete solution for that. Just copy/past code to a new WPF Application project.

Do not forget some assembly references i.e.

System.Web.Extention

System.Web.Script.Serialization

System.IO.IsolatedStorage;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MySettings settings = MySettings.Read(true);
            Debug.WriteLine("Current value of 'myInteger': " + settings.myInteger);
            Debug.WriteLine("Incrementing 'myInteger'...");
            settings.myInteger++;
            Debug.WriteLine("Saving settings...");
            settings.Save(true);
            Debug.WriteLine("Done.");

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            MySettings settings = MySettings.Read(true);
            Debug.WriteLine("Current value of 'myInteger': " + settings.myInteger);
            Debug.WriteLine("Incrementing 'myInteger'...");
            settings.myInteger++;
            settings.myString = "new S1";
            Debug.WriteLine("Saving settings...");
            settings.Save(true);
            Debug.WriteLine("Done.");

            lbl1.Content = settings.myString;
        }
    }

    class MySettings : AppSettings<MySettings>
    {
        public string myString = "Hello World";
        public int myInteger = 1;
    }

    public class AppSettings<T> where T : new()
    {
        private const string DEFAULT_FILENAME = "Settings";


        private static string GetSettingFileName
        {
            get
            {
                var exeName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                var fullFileName = string.Format("{0}_{1}.json", exeName, DEFAULT_FILENAME);
                return fullFileName;
            }
        }

        public void Save(bool isIsolatedStrorage)
        {
            if (isIsolatedStrorage)
            {
                try
                {
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(GetSettingFileName, FileMode.Create, IsolatedStorageFile.GetUserStoreForAssembly()))
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            writer.Write((new JavaScriptSerializer()).Serialize(this));
                        }
                    }
                }
                catch (Exception)
                {
                    return;
                }
            }
            else // We will create settings file in the same directory. All users will can access to it. Settings are non user independed.
            {
                File.WriteAllText(GetSettingFileName, (new JavaScriptSerializer()).Serialize(this));
            }
        }

        public static void Save(T pSettings, bool isIsolatedStrorage)
        {
            if (isIsolatedStrorage)
            {
                try
                {

                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(GetSettingFileName, FileMode.Create, IsolatedStorageFile.GetUserStoreForAssembly()))
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            writer.Write((new JavaScriptSerializer()).Serialize(pSettings));
                        }
                    }
                }
                catch (Exception)
                {
                    return;
                }
            }
            else // We will create settings file in the same directory. All users will can access to it. Settings are non user independed.
            {
                File.WriteAllText(GetSettingFileName, (new JavaScriptSerializer()).Serialize(pSettings));
            }
        }

        public static T Read(bool isIsolatedStrorage)
        {
            T t = new T();
            if (isIsolatedStrorage)
            {
                try
                {
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(GetSettingFileName, FileMode.Open, IsolatedStorageFile.GetUserStoreForAssembly()))
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        t = (new JavaScriptSerializer()).Deserialize<T>(reader.ReadToEnd());
                    }
                }
                catch (Exception)
                {
                }
            }
            else // We will create settings file in the same directory. All users will can access to it. Settings are non user independed.
            {
                if (File.Exists(GetSettingFileName))
                    t = (new JavaScriptSerializer()).Deserialize<T>(File.ReadAllText(GetSettingFileName));
            }

            return t;
        }
    }
}

You can find settings file created by path like

C:\Users\USER_NAME\AppData\Local\IsolatedStorage\cogrxuh4.gma\140esil0.t05\Url.bejzwgwbgheugcdwci5jbycjpt0p5rke\AssemFiles

Perhaps this part "cogrxuh4.gma\140esil0.t05\Url.bejzwgwbgheugcdwci5jbycjpt0p5rke" will be different in your case.

Also using this class you can keep all settings locally at the same directory where you have EXE file.

ANd finally you will need define some AppConfigurationManager class where you can implement all job asynchronously.