24
Sep

Load & Save File from Isolated Storage

Post a comment » Popularity: 1%

Mike Snow demonstrated how to read file and load file from local storage. Please read the C# for details.

Load & Save File from Isolated Storage

Save and read file from local storage:
using System;
using System.Windows.Controls;
using System.IO.IsolatedStorage;
using System.IO;

namespace SilverlightApplication10
{
    public class Page : UserControl
    {
        public Page()
        {
            SaveData("Hello There", "MyData.txt");
            if (HasFile("MyData.txt"))
            {
                string test = LoadData("MyData.txt");
            }
        }

        private void SaveData(string data, string fileName)
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf);
            using (StreamWriter sw = new StreamWriter(isfs))
            {
                sw.Write(data);
                sw.Close();
            }
        }

        private bool HasFile(string fileName)
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            return isf.FileExists(fileName);
        }

        private string LoadData(string fileName)
        {
            string data = String.Empty;
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf);
            using (StreamReader sr = new StreamReader(isfs))
            {
                string lineOfData = String.Empty;
                while ((lineOfData = sr.ReadLine()) != null) data += lineOfData;
            }
            return data;
        }
    }
}
  • DZone
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit

Related Posts

Leave a Reply