Freigeben über


How to: Find Existing Files and Directories in Isolated Storage 

You can also search for existing directories and files using an isolated storage file. Remember that within a store, file and directory names are specified with respect to the root of the virtual file system. Also, file and directory names in the Windows file systems are case-insensitive.

To search for a directory, use the GetDirectoryNames instance method of IsolatedStorageFile. GetDirectoryNames takes a string representing a search pattern. Both single-character (?) and multi-character (*) wildcard characters are supported. These wildcard characters cannot appear in the path portion of the name; that is, directory1/*ect* is a valid search string, but *ect*/directory2 is not.

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

Neither GetDirectoryNames nor GetFileNames is recursive; that is, IsolatedStorageFile does not supply methods for listing all directories or files in your store. However, examples of recursive methods are part of the code below. Also note that both GetDirectoryNames and GetFileNames return only the directory or file name of the item found. For example, if there is a match on the directory RootDir/SubDir/SubSubDir, SubSubDir will be returned in the results array.

FindingExistingFilesAndDirectories Example

The following code example illustrates how to create files and directories in an isolated store. First, a store 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 method 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 of the directories it has found. The result is all the directory names 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 of these directories for files using the GetFileNames method. The result is returned in an array for display.

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

Public Module modmain
   ' These arrayLists hold the directory and file names as they are found.

   Dim directoryList As New ArrayList()
   Dim fileList As New ArrayList()

   ' Retrieves an array of all directories in the store, and 
   ' displays the results.

   Sub Main()

      ' This part of the code sets up a few directories and files in the
      ' store.

      Dim isoStore As IsolatedStorageFile
      isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)

      isoStore.CreateDirectory("TopLevelDirectory")
      isoStore.CreateDirectory("TopLevelDirectory/SecondLevel")
      isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory")

      Dim aStream As New IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore)
      Dim bStream As New IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore)
      ' End of setup.

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

      Call GetAllDirectories("*", isoStore)

      Dim directory As String
      For Each directory In directoryList
         console.WriteLine(directory)
      Next

      Console.WriteLine()
      Console.WriteLine("Retrieve all the files in the directory by calling the GetFiles method.")
      Dim file As String

      Call GetAllFiles(isoStore)
      For Each file In fileList
         Console.WriteLine(file)
      Next

   End Sub

   Sub GetAllDirectories(ByVal pattern As String, ByVal storeFile As IsolatedStorageFile)

      ' Retrieve directories.

      Dim directories As String()
      directories = storeFile.GetDirectoryNames(pattern)
      Dim directory As String

      For Each directory In directories

         ' Add the directory to the final list.

         directoryList.Add((pattern.TrimEnd(CChar("*"))) + directory + "/")

         ' Call the method again using directory.

         GetAllDirectories((pattern.TrimEnd(CChar("*")) + directory + "/"), storeFile)

      Next

   End Sub

   Sub GetAllFiles(ByVal storefile As IsolatedStorageFile)

      ' This adds the root to the directory list.

      directoryList.Add("*")

      Dim directory As String

      For Each directory In directoryList

         Dim files As String()
         files = storefile.GetFileNames(directory + "*")
         Dim File As String

         For Each File In files
            fileList.Add(File)
         Next

      Next

   End Sub
End Module
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections;

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);
      isoStore.CreateDirectory("TopLevelDirectory");
      isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");
      isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");
      new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
      new IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore);
      // End of setup.

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

      foreach(string directory in GetAllDirectories("*", isoStore)){
         Console.WriteLine(directory);
      }
      Console.WriteLine('\r');

      // 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)){
         Console.WriteLine(file);
      }  

   }// End of Main.
  
   // Method to retrieve all directories, recursively, within a store.

   public static string[] GetAllDirectories(string pattern, IsolatedStorageFile storeFile){

      // Get the root of the search string.

      string root = Path.GetDirectoryName(pattern);

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

      // Retrieve directories.

      string[] directories;

      directories = storeFile.GetDirectoryNames(pattern);

      ArrayList directoryList = new ArrayList(directories);

      // Retrieve subdirectories of matches.

      for (int i = 0, max = directories.Length; i < max; i++){
         string directory = directoryList[i] + "/";
         string[] more = GetAllDirectories (root + directory + "*", storeFile);

         // For each subdirectory found, add in the base path.

         for (int j = 0; j < more.Length; 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.Length;
         max += more.Length;
      }

      return (string[])directoryList.ToArray(Type.GetType("System.String"));
   }

   public static string[] GetAllFiles(string pattern, IsolatedStorageFile storeFile){

      // Get the root and file portions of the search string.

      string fileString = Path.GetFileName(pattern);

      string[] files;
      files = storeFile.GetFileNames(pattern);

      ArrayList fileList = new ArrayList(files);

      // 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 (string[])fileList.ToArray(Type.GetType("System.String"));
         
   }// End of GetFiles.

}

See Also

Reference

IsolatedStorageFile

Other Resources

Performing Isolated Storage Tasks