This documentation is archived and is not being maintained.

How to: Read and Write to Files in Isolated Storage

There are a number of ways to open a file within a store using the IsolatedStorageFileStream class. Once an IsolatedStorageFileStream has been obtained, it can be used to get a StreamReader or StreamWriter. With the StreamReader and StreamWriter, you can read and write to a file in a store as you would to any other file. For a wider discussion of reading and writing to a file, see File and Stream I/O.

The following code example obtains an isolated store, creates a file named TestStore.txt, and writes "Hello Isolated Storage" to the file. The code then reads the file and prints the results to the console.

using namespace System;
using namespace System::IO;
using namespace System::IO::IsolatedStorage;

public ref class ReadingAndWritingToFiles
    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, (Type ^)nullptr, (Type ^)nullptr);

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

        array<String^>^ fileNames = isoStore->GetFileNames("TestStore.txt");
        for each (String^ file in fileNames)
            if (file == "TestStore.txt")
                Console::WriteLine("The file already exists!");
                Console::Write("Type \"StoreAdm /REMOVE\" at the command line to delete all ");
                Console::WriteLine("Isolated Storage for this user.");

                // Exit the program.
                return 0;


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

        // Call the readFromFile and write the returned string to the

        // Exit the program.
        return 0;
    } // End of main.

    // This method writes "Hello Isolated Storage" to the file.
    static void writeToFile(IsolatedStorageFile^ isoStore)
        // Declare a new StreamWriter.
        StreamWriter^ writer = nullptr;

        // Assign the writer to the store and the file TestStore.
        writer = gcnew StreamWriter(gcnew IsolatedStorageFileStream(
            "TestStore.txt", FileMode::CreateNew, isoStore));

        // Have the writer write "Hello Isolated Storage" to the store.
        writer->WriteLine("Hello Isolated Storage");


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

    } // End of writeToFile.

    // This method reads the first line in the "TestStore.txt" file.
    static String^ readFromFile(IsolatedStorageFile^ isoStore)
        // This code opens the TestStore.txt file and reads the string.
        StreamReader^ reader = gcnew StreamReader(gcnew IsolatedStorageFileStream(
            "TestStore.txt", FileMode::Open, isoStore));

        // Read a line from the file and add it to sb.
        String^ sb = reader->ReadLine();

        // Close the reader.

        // Return the string.
        return sb->ToString();

    } // End of readFromFile.

int main()
    return ReadingAndWritingToFiles::Main();