清除使用者資料檔案夾中的流覽資料

若要清除 WebView2 應用程式使用者資料檔案夾中的流覽資料並釋放空間,請呼叫 Clear Browsing Data API 的方法。

Clear Browsing Data API 可讓您以程式設計方式清除與 WebView2 使用者設定檔相關聯之 使用者資料檔案夾 中的資料。 例如,當使用者登出時,請使用此 API 來清除使用者資料和歷程記錄。

您可以:

  • 清除所有流覽資料。
  • 清除選取的流覽資料類型。
  • 清除指定時間範圍內選取的流覽資料類型。

清除所有流覽資料

這個方法會清除資料類型列舉中列出的所有流覽資料類型,不論資料的建立時間為何。 它會從呼叫 方法之使用者設定檔的使用者資料檔案夾中清除資料。

清除選取的流覽資料類型

這個方法會清除指定的流覽資料類型,不論資料的建立時間為何。 它會從呼叫 方法之使用者設定檔的使用者資料檔案夾中清除資料。

清除時間範圍內選取的流覽資料類型

這個方法會清除在指定的開始時間和結束時間之間建立的指定流覽資料類型。 它會從呼叫 方法之使用者設定檔的使用者資料檔案夾中清除資料。

範例:清除時間範圍內選取的流覽資料類型

此範例會清除過去一小時內的自動填入資料和密碼自動儲存資料。

下列參數值會傳遞至 Clear Browsing Data API 方法:

  • 選取的瀏覽器資料類型 = 自動填入資料和密碼自動儲存資料。

  • 指定的時間範圍 = 過去一小時 (3600 秒) 。

// Clears autofill data.
private void ClearAutofillData()
{
    CoreWebView2Profile profile;
    if (webView.CoreWebView2 != null)
    {
        profile = webView.CoreWebView2.Profile;
        // Get the current time, the time in which the browsing data will be cleared
        // until.
        System.DateTime endTime = DateTime.Now;
        System.DateTime startTime = DateTime.Now.AddHours(-1);
        // Offset the current time by one hour to clear the browsing data from the
        // last hour.
        CoreWebView2BrowsingDataKinds dataKinds = (CoreWebView2BrowsingDataKinds)
                                 (CoreWebView2BrowsingDataKinds.GeneralAutofill | 
                                  CoreWebView2BrowsingDataKinds.PasswordAutosave);
        await profile.ClearBrowsingDataAsync(dataKinds, startTime, endTime);
    }
}

Api:

API 參照

另請參閱