Export (0) Print
Expand All

Deleting Files and Directories

You can delete directories and files within an isolated storage file. Remember that within a store, file and directory names are operating-system dependent (usually not case-sensitive on Microsoft Windows systems) and are specified with respect to the root of the virtual file system.

The IsolatedStoreFile class supplies two instance methods for deleting directories and files, DeleteDirectory and DeleteFile. An IsolatedStorageFileException is thrown if you try to delete a file or directory that does not exist. If a wildcard character is included in the name, then DeleteDirectory throws an IsolatedStorageFileException while DeleteFile throws an ArgumentException.

DeleteDirectory fails if the directory contains any files or subdirectories. As part of the DeletingFilesAndDirectories example, a method is defined that deletes all the contents of a directory and then the directory itself. Likewise, you could define your own DeleteFiles method that accepts a wildcard character by using the GetFileNames method to get a list of all matching files, and then deleting each one in turn. For more information on searching the virtual file system of a store, see Finding Existing Files and Directories.

DeletingFilesAndDirectories Example

The following code example creates and then deletes several directories and files.

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

Public Module modmain

   Sub Main()

      ' Get an isolated store for this assembly and put it into an
      ' IsolatedStoreFile object.

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

      Console.WriteLine("Creating Directories:")

      ' This code creates several different directories.

      isoStore.CreateDirectory("TopLevelDirectory")
      Console.WriteLine("TopLevelDirectory")
      isoStore.CreateDirectory("TopLevelDirectory/SecondLevel")
      Console.WriteLine("TopLevelDirectory/SecondLevel")


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

isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory")
      Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory")
      Console.WriteLine()

      ' This code creates a few files and places them in the directories.

      Console.WriteLine("Creating Files:")

      ' This file is placed in the root.

      Dim isoStream1 As New IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore)
      Console.WriteLine("InTheRoot.txt")

      isoStream1.Close()

      ' This file is placed in the InsideDirectory.

      Dim isoStream2 As New IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore)

Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt")
      Console.WriteLine()
      isoStream2.Close()

      Console.WriteLine("Deleting File:")

      ' This code deletes the HereIAm.txt file.

isoStore.DeleteFile("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt")

Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt")
      Console.WriteLine()

      Console.WriteLine("Deleting Directory:")

      ' This code deletes the InsideDirectory.

isoStore.DeleteDirectory("AnotherTopLevelDirectory/InsideDirectory/")
      Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory/")
      Console.WriteLine()

   End Sub

End Module

[C#]
using System;
using System.IO.IsolatedStorage;
using System.IO;

public class DeletingFilesDirectories{

   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);
    
      Console.WriteLine("Creating Directories:");

      // This code creates several different directories.

      isoStore.CreateDirectory("TopLevelDirectory");
      Console.WriteLine("TopLevelDirectory");
      isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");
      Console.WriteLine("TopLevelDirectory/SecondLevel");

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

      isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");
      Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory");
      Console.WriteLine();

      // This code creates a few files and places them in the directories.

      Console.WriteLine("Creating Files:");

      // This file is placed in the root.

      IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
      Console.WriteLine("InTheRoot.txt");
  
      isoStream1.Close();

      // This file is placed in the InsideDirectory.

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

      isoStream2.Close();

      Console.WriteLine("Deleting File:");

      // This code deletes the HereIAm.txt file.
      isoStore.DeleteFile("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt");
      Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt"); 
      Console.WriteLine();

      Console.WriteLine("Deleting Directory:");

      // This code deletes the InsideDirectory.

      isoStore.DeleteDirectory("AnotherTopLevelDirectory/InsideDirectory/");
      Console.WriteLine("AnotherTopLevelDirectory/InsideDirectory/");
      Console.WriteLine();

   }// End of main.

}

See Also

Performing Isolated Storage Tasks | IsolatedStorageFile

Show:
© 2015 Microsoft