Export (0) Print
Expand All

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.

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

Public Class CreatingFilesDirectories
    Public Shared 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.


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


        ' 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.")

        ' 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.")

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

Community Additions

© 2015 Microsoft