IsolatedStorageFile.DeleteFile(String) 方法

定义

删除独立存储范围中的文件。

public:
 void DeleteFile(System::String ^ file);
public void DeleteFile (string file);
member this.DeleteFile : string -> unit
Public Sub DeleteFile (file As String)

参数

file
String

要在独立存储范围中删除的文件的相对路径。

例外

目标文件已打开或者路径不正确。

文件路径为 null

示例

下面的代码示例使用 DeleteFile 方法删除独立存储中的多个文件。

void DeleteFiles()
{
   
   try
   {
      IsolatedStorageFile^ isoFile = IsolatedStorageFile::GetStore( static_cast<IsolatedStorageScope>(IsolatedStorageScope::User | IsolatedStorageScope::Assembly | IsolatedStorageScope::Domain), System::Security::Policy::Url::typeid, System::Security::Policy::Url::typeid );
      array<String^>^dirNames = isoFile->GetDirectoryNames( "*" );
      array<String^>^fileNames = isoFile->GetFileNames( "*" );
      
      // List the files currently in this Isolated Storage.
      // The list represents all users who have personal
      // preferences stored for this application.
      if ( fileNames->Length > 0 )
      {
         for ( int i = 0; i < fileNames->Length; ++i )
         {
            
            //Delete the files.
            isoFile->DeleteFile( fileNames[ i ] );

         }
         fileNames = isoFile->GetFileNames( "*" );
      }
      isoFile->Close();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->ToString() );
   }

}
public void DeleteFiles()
{
    try
    {
        IsolatedStorageFile isoFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
            IsolatedStorageScope.Assembly |
            IsolatedStorageScope.Domain,
            typeof(System.Security.Policy.Url),
            typeof(System.Security.Policy.Url));

        String[] dirNames = isoFile.GetDirectoryNames("*");
        String[] fileNames = isoFile.GetFileNames("*");

        // List the files currently in this Isolated Storage.
        // The list represents all users who have personal
        // preferences stored for this application.
        if (fileNames.Length > 0)
        {
            for (int i = 0; i < fileNames.Length; ++i)
            {
                // Delete the files.
                isoFile.DeleteFile(fileNames[i]);
            }
            // Confirm that no files remain.
            fileNames = isoFile.GetFileNames("*");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}
Public Sub DeleteFiles()
    Try
        Dim isoFile As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or _
            IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, _
            GetType(System.Security.Policy.Url), GetType(System.Security.Policy.Url))
        Dim name As String
        Dim dirNames As String() = isoFile.GetDirectoryNames("*")
        Dim fileNames As String() = isoFile.GetFileNames("*")
        ' List the files currently in this Isolated Storage.
        ' The list represents all users who have personal
        ' preferences stored for this application.
        If fileNames.Length > 0 Then
            For Each name In fileNames
                ' Delete the files.
                isoFile.DeleteFile(name)
            Next name
            'Confirm no files are left.
            fileNames = isoFile.GetFileNames("*")
        End If
    Catch ex As Exception
        Console.WriteLine(ex.ToString())
    End Try
End Sub

注解

删除后无法恢复已删除的文件。

如何:删除独立存储中的文件和目录示例演示了 方法的DeleteFile用法。

适用于

另请参阅