Share via


How to: Create Files and Directories in Isolated StorageĀ 

After you have obtained a store, you can create directories and files for storing data. Within a store, file and directory names are specified with respect to the root of the virtual file system.

To create a directory, use the CreateDirectory instance method of IsolatedStorageFile. If you specify a subdirectory of an uncreated directory, then both directories are created. If you specify a directory that already exists, no exception is generated. However, if you specify a directory name that contains invalid characters, an IsolatedStorageException is generated.

To create and open a file, use one of the IsolatedStorageFileStream constructors, passing in the file name, the FileMode value OpenOrCreate, and the store in which you want the file created. Then, you can do the things you would expect to do with data in a file stream, such as read, seek, and write. The IsolatedStorageFileStream constructor can also be used to open a file for other purposes.

You can create or open files without first obtaining a store by using any of the IsolatedStorageFileStream constructors that do not take an IsolatedStorageFile argument. When you use this form of the constructor, the file is created in the domain store for the file.

In the Windows file systems, isolated storage file and directory names are case-insensitive for the purposes of comparing names. Thus, if you create a file named ThisFile.txt and then create another file named THISFILE.TXT, only one file is created. The file name keeps its original casing for display purposes.

CreatingFilesAndDirectories Example

The following code example illustrates how to create files and directories in an isolated store. First, a store isolated by user, domain, and assembly is retrieved and placed in the isoStore variable. The CreateDirectory method is used to set up a few different directories, and the IsolatedStorageFileStream method creates some files in these directories.

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.
}

See Also

Reference

IsolatedStorageFile
IsolatedStorageFileStream

Other Resources

Performing Isolated Storage Tasks