Csharp/CSharp Tutorial/File Directory Stream/IsolatedStorage

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create Directory in your Isolated Storage File

using System;
using System.IO;
using System.IO.IsolatedStorage;
static class MainClass
{
    static void Main()
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly())
        {
            store.CreateDirectory("MyFolder");
            Console.WriteLine("Current size: " + store.CurrentSize.ToString());
            Console.WriteLine("Scope: " + store.Scope.ToString());
        }
    }
}
Current size: 2048
Scope: User, Assembly

Get included files in your Isolated Storage File

using System;
using System.IO;
using System.IO.IsolatedStorage;
static class MainClass
{
    static void Main()
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly())
        {
            Console.WriteLine("Contained files include:");
            string[] files = store.GetFileNames("*.*");
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }
        }
    }
}
Contained files include:
MyFile.txt

Open up isolated storage based on identity of user + assembly evidence

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.IsolatedStorage;
using System.Security.Permissions;
[assembly: IsolatedStorageFilePermission(SecurityAction.RequestMinimum, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
public class MainClass{
    public static void Main()
    {
      using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly())
      {
        using (IsolatedStorageFileStream isStream = new IsolatedStorageFileStream("MyData.txt",FileMode.OpenOrCreate, store))
        {
          using (StreamWriter sw = new StreamWriter(isStream))
          {
            sw.WriteLine("This is my data.");
            sw.WriteLine("Cool, huh?");
          }
        }
      }
    }
}

Read from IsolatedStorage

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
    public class Tester
    {
        public static void Main()
        {
            Tester app = new Tester();
            app.Run();
        }
        private void Run()
        {
            IsolatedStorageFileStream configFile = new IsolatedStorageFileStream("Tester.cfg", FileMode.Open);
            StreamReader reader = new StreamReader(configFile);
            string theEntry;
            do
            {
                theEntry = reader.ReadLine();
                Console.WriteLine(theEntry);
            } while (theEntry != null);
            reader.Close();
            configFile.Close();
        }
    }

Read text in From IsoStorage

using System;
using System.IO.IsolatedStorage;
using System.IO;
  class MainClass
  {
    static void Main(string[] args)
    {
      using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly())
      {
        using (IsolatedStorageFileStream isStream = new IsolatedStorageFileStream("MyData.txt", FileMode.Open,FileAccess.Read, store))
        {
          using (StreamReader sr = new StreamReader(isStream))
          {
            string allTheData = sr.ReadToEnd();
            Console.WriteLine(allTheData);
          }
        }
      }

    }
  }

Save file to Isolated Storage File

using System;
using System.IO;
using System.IO.IsolatedStorage;
static class MainClass
{
    static void Main()
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly())
        {
            store.CreateDirectory("MyFolder");
            using (Stream fs = new IsolatedStorageFileStream("MyFile.txt", FileMode.Create, store))
            {
                StreamWriter w = new StreamWriter(fs);
                w.WriteLine("Test");
                w.Flush();
            }
        }
    }
}

Write text out To IsoStorage

using System;
using System.IO.IsolatedStorage;
using System.IO;
  class MainClass
  {
    static void Main(string[] args)
    {
      using (IsolatedStorageFile store =IsolatedStorageFile.GetUserStoreForAssembly())
      {
        using (IsolatedStorageFileStream isStream = new IsolatedStorageFileStream("MyData.txt",FileMode.OpenOrCreate, store))
        {
          using (StreamWriter sw = new StreamWriter(isStream))
          {
            sw.WriteLine("data.");
            sw.WriteLine("Cool?");
          }
        }
      }

    }
  }

Writing To Isolated Storage

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;

    public class Tester
    {
        public static void Main()
        {
            IsolatedStorageFileStream configFile =new IsolatedStorageFileStream("Tester.cfg", FileMode.Create);
            StreamWriter writer = new StreamWriter(configFile);
            String output;
            System.DateTime currentTime = System.DateTime.Now;
            output = "Last access: " + currentTime.ToString();
            writer.WriteLine(output);
            output = "Last position = 27,35";
            writer.WriteLine(output);
            writer.Close();
            configFile.Close();
        }
    }