This documentation is archived and is not being maintained.

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.

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.

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.


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

        // 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.");

        // This file is placed in the InsideDirectory.

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

        Console.WriteLine("Created a new file in the InsideDirectory.");
    } // End of Main.