Практическое руководство. Считывание из файлов и запись в файлы в изолированном хранилище

Для чтения из файла или записи в файл в изолированном хранилище используется объект IsolatedStorageFileStream с модулем чтения потока (объект StreamReader ) или модулем записи в поток (объект StreamWriter).

Пример

Следующий пример создает изолированное хранилище и проверяет, существует ли в хранилище файл с именем TestStore.txt. Если он не существует, код создает файл и записывает в файл текст "Hello Isolated Storage". Если TestStore.txt уже существует, пример кода выполняет чтение из файла.

using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

            if (isoStore.FileExists("TestStore.txt"))
            {
                Console.WriteLine("The file already exists!");
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.Open, isoStore))
                {
                    using (StreamReader reader = new StreamReader(isoStream))
                    {
                        Console.WriteLine("Reading contents:");
                        Console.WriteLine(reader.ReadToEnd());
                    }
                }
            }
            else
            {
                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew, isoStore))
                {
                    using (StreamWriter writer = new StreamWriter(isoStream))
                    {
                        writer.WriteLine("Hello Isolated Storage");
                        Console.WriteLine("You have written to the file.");
                    }
                }
            }
        }
    }
}
Imports System.IO
Imports System.IO.IsolatedStorage

Module Module1

    Sub Main()
        Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly, Nothing, Nothing)

        If (isoStore.FileExists("TestStore.txt")) Then
            Console.WriteLine("The file already exists!")
            Using isoStream As IsolatedStorageFileStream = New IsolatedStorageFileStream("TestStore.txt", FileMode.Open, isoStore)
                Using reader As StreamReader = New StreamReader(isoStream)
                    Console.WriteLine("Reading contents:")
                    Console.WriteLine(reader.ReadToEnd())
                End Using
            End Using
        Else
            Using isoStream As IsolatedStorageFileStream = New IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew, isoStore)
                Using writer As StreamWriter = New StreamWriter(isoStream)
                    writer.WriteLine("Hello Isolated Storage")
                    Console.WriteLine("You have written to the file.")
                End Using
            End Using
        End If
    End Sub

End Module

См. также