How to: Find Existing Files and Directories in Isolated Storage


To search for a directory in isolated storage, use the IsolatedStorageFile.GetDirectoryNames method. This method takes a string that represents a search pattern. You can use both single-character (?) and multi-character (*) wildcard characters in the search pattern, but the wildcard characters must appear in the final portion of the name. For example, directory1/*ect* is a valid search string, but *ect*/directory2 is not.

To search for a file, use the IsolatedStorageFile.GetFileNames method. The restriction for wildcard characters in search strings that applies to GetDirectoryNames also applies to GetFileNames.

Neither of these methods is recursive; the IsolatedStorageFile class does not supply any methods for listing all directories or files in your store. However, recursive methods are shown in the following code example.

The following code example illustrates how to create files and directories in an isolated store. First, a store that is isolated for 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(String, FileMode, IsolatedStorageFile) constructor creates some files in these directories. The code then loops through the results of the GetAllDirectories method. This method uses GetDirectoryNames to find all the directory names in the current directory. These names are stored in an array, and then GetAllDirectories calls itself, passing in each directory it has found. As a result, all the directory names are returned in an array. Next, the code calls the GetAllFiles method. This method calls GetAllDirectories to find out the names of all the directories, and then it checks each directory for files by using the GetFileNames method. The result is returned in an array for display.

using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections;
using System.Collections.Generic;

public class FindingExistingFilesAndDirectories
    // Retrieves an array of all directories in the store, and
    // displays the results.
    public static void Main()
        // This part of the code sets up a few directories and files in the
        // store.
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
            IsolatedStorageScope.Assembly, null, null);
        // End of setup.

        Console.WriteLine("Here is a list of all directories in this isolated store:");

        foreach (string directory in GetAllDirectories("*", isoStore))

        // Retrieve all the files in the directory by calling the GetFiles
        // method.

        Console.WriteLine("Here is a list of all the files in this isolated store:");
        foreach (string file in GetAllFiles("*", isoStore)){

    } // End of Main.

    // Method to retrieve all directories, recursively, within a store.
    public static List<String> GetAllDirectories(string pattern, IsolatedStorageFile storeFile)
        // Get the root of the search string.
        string root = Path.GetDirectoryName(pattern);

        if (root != "")
            root += "/";

        // Retrieve directories.
        List<String> directoryList = new List<String>(storeFile.GetDirectoryNames(pattern));

        // Retrieve subdirectories of matches.
        for (int i = 0, max = directoryList.Count; i < max; i++)
            string directory = directoryList[i] + "/";
            List<String> more = GetAllDirectories(root + directory + "*", storeFile);

            // For each subdirectory found, add in the base path.
            for (int j = 0; j < more.Count; j++)
                more[j] = directory + more[j];

            // Insert the subdirectories into the list and
            // update the counter and upper bound.
            directoryList.InsertRange(i + 1, more);
            i += more.Count;
            max += more.Count;

        return directoryList;

    public static List<String> GetAllFiles(string pattern, IsolatedStorageFile storeFile)
        // Get the root and file portions of the search string.
        string fileString = Path.GetFileName(pattern);

        List<String> fileList = new List<String>(storeFile.GetFileNames(pattern));

        // Loop through the subdirectories, collect matches,
        // and make separators consistent.
        foreach (string directory in GetAllDirectories("*", storeFile))
            foreach (string file in storeFile.GetFileNames(directory + "/" + fileString))
                fileList.Add((directory + "/" + file));

        return fileList;
    } // End of GetFiles.

Isolated Storage