語言

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 刪除隔離儲存中多個檔案的方法。

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方法的使用。

適用於

另請參閱