如何:在独立存储中删除文件和目录
可以删除独立存储文件中的目录和文件。 在存储区中,文件名和目录名依赖于操作系统,并指定为虚拟文件系统根目录的相对路径。 在 Windows 操作系统中,它们不区分大小写。
System.IO.IsolatedStorage.IsolatedStorageFile 类提供了两个用于删除目录和文件的方法:DeleteDirectory 和 DeleteFile。 如果尝试删除并不存在的文件和目录,则会引发 IsolatedStorageException 异常。 如果在名称中包含通配符,DeleteDirectory 将引发 IsolatedStorageException 异常,并且 DeleteFile 会引发 ArgumentException 异常。
如果目录中包含任何文件或子目录,DeleteDirectory 方法将会失败。 可以使用 GetFileNames 和 GetDirectoryNames 方法检索现有文件和目录。 若要详细了解如何搜索存储的虚拟文件系统,请参阅如何:在独立存储中查找现有文件和目录。
示例
下面的代码示例创建并删除多个目录和文件。
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