若要清除 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: