方法 : 分離ストレージ内でファイルの読み取りと書き込みを行う

IsolatedStorageFileStream クラスを使用してストア内のファイルを開くには、いくつかの方法があります。IsolatedStorageFileStream を取得した後、それを使用して StreamReader または StreamWriter を取得できます。StreamReaderStreamWriter を使用すると、ほかのファイルの場合と同じ方法でストア内のファイルの読み取りや書き込みを行うことができます。ファイルの読み取りと書き込みの詳細については、「ファイルおよびストリーム入出力」を参照してください。

ReadingAndWritingToFiles の例

分離ストアを取得し、TestStore.txt という名前のファイルを作成し、そのファイルに "Hello Isolated Storage" と書き込むコード例を次に示します。その後で、ファイルを読み取り、結果をコンソールに出力します。

Imports System
Imports System.IO
Imports System.IO.IsolatedStorage

Public Module modmain

   Sub Main()

      ' Get an isolated store for this assembly and put it into an
      ' IsolatedStoreFIle object.

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

      ' This code checks to see if the file already exists.

      Dim filenames As String()
      filenames = isoStore.GetFileNames("TestStore.txt")

      Dim file As String
      For Each file In filenames
         If file = "TestStore.txt" Then
            Console.WriteLine("The file already exists!")
            Console.WriteLine("Type ""StoreAdm /REMOVE"" at the command line to delete all Isolated Storage for this user.")
            Exit Sub

         End If
      Next

      WriteToFile(isoStore)

      Console.WriteLine("The file ""TestStore.txt"" contains:")

      ' Call readFromFile and write the returned string to the console.

      Console.WriteLine(ReadFromFile(isoStore))

   End Sub

   ' This method writes "Hello Isolated Storage" to the file.

   Private Sub WriteToFile(ByVal isoStore As IsolatedStorageFile)
      ' Declare a new StreamWriter.
      Dim writer As New StreamWriter(New IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew, isoStore))

      ' Have the writer write "Hello Isolated Storage" to the store.

      writer.WriteLine("Hello Isolated Storage")

      writer.Close()

      Console.WriteLine("You have written to the file.")
   End Sub

   ' This method reads the first line in the "TestStore.txt" file.

   Private Function ReadFromFile(ByVal isoStore As IsolatedStorageFile) As String

      ' This code opens the TestStore.txt file and reads the string.

      Dim reader As New StreamReader(New IsolatedStorageFileStream("TestStore.txt", FileMode.Open, isoStore))

      ' Read a line from the file and add it to sb.

      Dim sb As String
      sb = reader.ReadLine

      ' Close the reader.

      reader.Close()

      Return sb
   End Function

End Module
using System;
using System.IO;
using System.IO.IsolatedStorage;

public class ReadingAndWritingToFiles{

   public static int Main(){

      // Get an isolated store for this assembly and put it into an
      // IsolatedStoreFile object.

      IsolatedStorageFile isoStore =  IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);

      // This code checks to see if the file already exists.

      string[] fileNames = isoStore.GetFileNames("TestStore.txt");
      foreach (string file in fileNames){
         if(file == "TestStore.txt"){

            Console.WriteLine("The file already exists!");
            Console.WriteLine("Type \"StoreAdm /REMOVE\" at the command line to delete all Isolated Storage for this user.");

            // Exit the program.

            return 0;
         }
      }

      writeToFile(isoStore);

      Console.WriteLine("The file \"TestStore.txt\" contains:");
      // Call the readFromFile and write the returned string to the
      //console.

      Console.WriteLine(readFromFile(isoStore));

      // Exit the program.

      return 0;

   }// End of main.


   // This method writes "Hello Isolated Storage" to the file.

   private static void writeToFile(IsolatedStorageFile isoStore){

      // Declare a new StreamWriter.

      StreamWriter writer = null;

      // Assign the writer to the store and the file TestStore.

      writer = new StreamWriter(new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew,isoStore));

      // Have the writer write "Hello Isolated Storage" to the store.

      writer.WriteLine("Hello Isolated Storage");

      writer.Close();
      
      Console.WriteLine("You have written to the file.");

   }// End of writeToFile.


   // This method reads the first line in the "TestStore.txt" file.

   public static String readFromFile(IsolatedStorageFile isoStore){

      // This code opens the TestStore.txt file and reads the string.

      StreamReader reader = new StreamReader(new IsolatedStorageFileStream("TestStore.txt", FileMode.Open,isoStore));

      // Read a line from the file and add it to sb.

      String sb = reader.ReadLine();

      // Close the reader.

      reader.Close();

      // Return the string.

      return sb.ToString();

   }// End of readFromFile.
}

参照

関連項目

IsolatedStorageFile
IsolatedStorageFileStream
System.IO.FileMode
System.IO.FileAccess
System.IO.StreamReader
System.IO.StreamWriter

その他の技術情報

分離ストレージ作業の実行