Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to: Delete Files and Directories in Isolated Storage

You can delete directories and files within an isolated storage file. Within a store, file and directory names are operating-system dependent and are specified as relative to the root of the virtual file system. They are not case-sensitive on Windows operating systems.

The System.IO.IsolatedStorage.IsolatedStorageFile class supplies two methods for deleting directories and files: DeleteDirectory and DeleteFile. An IsolatedStorageException exception is thrown if you try to delete a file or directory that does not exist. If you include a wildcard character in the name, DeleteDirectory throws an IsolatedStorageException exception, and DeleteFile throws an ArgumentException exception.

The DeleteDirectory method fails if the directory contains any files or subdirectories. You can use the GetFileNames and GetDirectoryNames methods to retrieve the existing files and directories. For more information about searching the virtual file system of a store, see How to: Find Existing Files and Directories in Isolated Storage.

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

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

Public Class DeletingFilesDirectories
    Public Shared Sub Main()
        ' Get a new isolated store for this user domain and assembly. 
        ' Put the store into an isolatedStorageFile object. 

        Dim isoStore As IsolatedStorageFile =  IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or
            IsolatedStorageScope.Domain 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 of main.
End Class
Show:
© 2015 Microsoft