Como: Excluir arquivos e diretórios no armazenamento isolado

Você pode excluir pastas e arquivos em um arquivo de armazenamento isolado. Em um repositório, nomes de arquivo e diretório são dependentes do sistema operacional e são especificados como relativos à raiz do sistema de arquivos virtual. Eles não diferenciam maiúsculas de minúsculas em sistemas operacionais Windows.

A classe System.IO.IsolatedStorage.IsolatedStorageFile fornece dois métodos para excluir arquivos e diretórios: DeleteDirectory e DeleteFile. Uma exceção IsolatedStorageException será gerada se você tentar excluir um arquivo ou um diretório que não exista. Se você incluir um caractere curinga no nome, DeleteDirectory gerará uma exceção IsolatedStorageException e DeleteFile gerará uma exceção ArgumentException.

O método DeleteDirectory falhará se o diretório contiver arquivos ou subpastas. Você pode usar os métodos GetFileNames e GetDirectoryNames para recuperar os arquivos e diretórios existentes. Para saber mais sobre o sistema de arquivos virtual de um repositório de pesquisa, confira Como localizar arquivos e diretórios existentes no armazenamento isolado.

Exemplo

O exemplo de código a seguir cria e exclui vários diretórios e arquivos.

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

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

        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 = gcnew IsolatedStorageFileStream("InTheRoot.txt",
            FileMode::Create, isoStore);
        Console::WriteLine("InTheRoot.txt");

        isoStream1->Close();

        // This file is placed in the InsideDirectory.

        IsolatedStorageFileStream^ isoStream2 = gcnew 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.
};

int main()
{
    DeletingFilesDirectories::Main();
}
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.
}
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 Class

Confira também