共用方式為


刪除檔案和目錄

您可以刪除隔離儲存區檔案內的目錄和檔案。請記得,在存放區內,目錄和檔案名稱是與作業系統有關的 (在 Microsoft Windows 系統上通常不是區分大小寫的),並且是相對於虛擬檔案系統的根目錄來指定的。

IsolatedStoreFile 類別提供兩個刪除目錄和檔案的執行個體方法:DeleteDirectoryDeleteFile。如果您嘗試刪除不存在的檔案或目錄,就會擲回 IsolatedStorageFileException。如果萬用字元包含在名稱中,DeleteDirectoryDeleteFile 擲回 ArgumentException 的時候,會擲回 IsolatedStorageFileException

如果目錄包含任何檔案或子目錄,DeleteDirectory 方法就會失敗。這個方法是 DeletingFilesAndDirectories 範例的一部分,是定義為要刪除目錄的所有內容及目錄本身。相同地,您可以使用 GetFileNames 方法取得所有相符檔案的清單,接著依次刪除每個檔案,自行定義接受萬用字元的 DeleteFiles 方法。如需搜尋存放區的虛擬檔案系統的詳細資訊,請參閱尋找現有檔案和目錄

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

其他資源

執行隔離儲存區工作