如何:在独立存储中删除文件和目录

更新:2007 年 11 月

您可以删除独立存储文件中的目录和文件。请记住,在存储区中,文件名和目录名是与操作系统相关的(在 Microsoft Windows 系统中通常不区分大小写),并且是根据虚文件系统的根目录具体而定的。

IsolatedStoreFile 类提供了两种删除目录和文件的实例方法:DeleteDirectoryDeleteFile。如果尝试删除并不存在的文件和目录,则会引发 IsolatedStorageFileException。如果名称中包含有通配符,则 DeleteDirectory 会引发 IsolatedStorageFileException,而 DeleteFile 将引发 ArgumentException

如果目录中包含任何文件或子目录,DeleteDirectory 将会失败。在 DeletingFilesAndDirectories 示例的一部分中定义了一个方法,该方法删除目录中的所有内容,然后删除目录本身。同样,您可以自己定义一个接受通配符的 DeleteFiles 方法,方法为:使用 GetFileNames 方法获取所有匹配文件的列表,然后依次删除每个文件。有关搜索存储区的虚文件系统的更多信息,请参见查找现有的文件和目录

DeletingFilesAndDirectories 示例

下面的代码示例先创建若干个目录和文件,然后将它们删除。

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
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.

}

请参见

参考

IsolatedStorageFile

其他资源

执行独立存储任务