Procédure : supprimer des fichiers et des répertoires dans un stockage isolé
Vous pouvez supprimer des répertoires et des fichiers dans un fichier de stockage isolé. Dans un magasin, les noms de répertoires et de fichiers dépendent du système d’exploitation et sont spécifiées par rapport à la racine du système de fichiers virtuel. Ils ne sont pas sensibles à la casse sur les systèmes d’exploitation Windows.
La classe System.IO.IsolatedStorage.IsolatedStorageFile fournit deux méthodes pour supprimer des répertoires et des fichiers : DeleteDirectory et DeleteFile. Une exception IsolatedStorageException est levée si vous essayez de supprimer un fichier ou un répertoire qui n’existe pas. Si vous incluez un caractère générique dans le nom, DeleteDirectory lève une exception IsolatedStorageException et DeleteFile lève une exception ArgumentException.
La méthode DeleteDirectory échoue si le répertoire contient des fichiers ou des sous-répertoires. Vous pouvez utiliser les méthodes GetFileNames et GetDirectoryNames pour récupérer les fichiers et les répertoires existants. Pour plus d’informations sur la recherche dans le système de fichiers virtuel d’un magasin, consultez Comment : rechercher des fichiers et des répertoires existants dans un stockage isolé.
Exemple
L’exemple de code suivant crée et supprime ensuite plusieurs fichiers et répertoires.
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