方法 : 分離ストレージでファイルおよびディレクトリを作成する

ストアを取得した後で、データを格納するためのディレクトリおよびファイルを作成できます。ストア内では、ファイル名およびディレクトリ名が、仮想ファイル システムのルートからのパスを使用して指定されています。

ディレクトリを作成するには、IsolatedStorageFileCreateDirectory インスタンス メソッドを使用します。作成されていないディレクトリのサブディレクトリを指定すると、ディレクトリとサブディレクトリの両方が作成されます。既存のディレクトリを指定した場合でも、例外は生成されません。ただし、指定したディレクトリに無効な文字が含まれている場合は、IsolatedStorageException が生成されます。

ファイルを作成したり、開いたりするには、IsolatedStorageFileStream コンストラクタの 1 つを使用し、ファイル名、FileMode 値、OpenOrCreate、ファイルの作成先のストアを渡します。その後で、ファイル ストリーム内のデータに関係する読み取り、シーク、書き込みなどの必要な操作を実行します。IsolatedStorageFileStream コンストラクタは、ファイルを別の用途で開く場合にも使用できます。

IsolatedStorageFile 引数を受け取らない IsolatedStorageFileStream コンストラクタを使用すると、あらかじめストアを取得せずに、ファイルを作成したり、開いたりできます。この形態のコンストラクタを使用したときは、ファイルはそのファイルのドメイン ストア内に作成されます。

Windows ファイル システムでは、分離ストレージ ファイル名やディレクトリ名は、名前比較の目的では大文字と小文字が区別されません。つまり、ThisFile.txt という名前のファイルを作成した後で、THISFILE.TXT という名前のファイルを作成しても、ファイルは 1 つしか作成されません。表示目的では、ファイル名の元の大文字/小文字が維持されます。

CreatingFilesAndDirectories の例

分離ストア内にファイルおよびディレクトリを作成する方法を次のコード例で示します。最初に、ユーザー、ドメイン、およびアセンブリ別に分離されたストアを取得し、isoStore 変数に格納します。CreateDirectory メソッドを使用して複数の異なるディレクトリを作成し、IsolatedStorageFileStream メソッドを使用してそれらのディレクトリにいくつかのファイルを作成します。

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

Public Module modmain

   Sub Main()

      ' Get an isolated store for user, domain, and assembly and put it into 
      ' an IsolatedStorageFile object.

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

      ' This code creates a few different directories.

      isoStore.CreateDirectory("TopLevelDirectory")
      isoStore.CreateDirectory("TopLevelDirectory/SecondLevel")

      ' This code creates two new directories, one inside the other.

      isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory")

      ' This file is placed in the root.

      Dim isoStream1 As New IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore)
      Console.WriteLine("Created a new file in the root.")
      isoStream1.Close()

      ' This file is placed in the InsideDirectory.

      Dim isoStream2 As New IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore)
      Console.WriteLine("Created a new file in the root.")
      isoStream2.Close()

      Console.WriteLine("Created a new file in the InsideDirectory.")

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

public class CreatingFilesDirectories{

   public static void Main(){

      // Get a new isolated store for this user, domain, and assembly.  
      // Put the store into an IsolatedStorageFile object.

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

      // This code creates a few different directories.

      isoStore.CreateDirectory("TopLevelDirectory");
      isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");

      // This code creates two new directories, one inside the other.
      isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");


      // This file is placed in the root.

      IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
      Console.WriteLine("Created a new file in the root.");
      isoStream1.Close();

      // This file is placed in the InsideDirectory.

      IsolatedStorageFileStream isoStream2 = new IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore);
      isoStream2.Close();

      Console.WriteLine("Created a new file in the InsideDirectory.");

   }// End of Main.
}

参照

関連項目

IsolatedStorageFile
IsolatedStorageFileStream

その他の技術情報

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