管理用户数据文件夹Manage the User Data Folder

WebView2 应用程序与用户数据文件夹交互,以存储浏览器数据,如 Cookie、权限和缓存的资源。WebView2 applications interact with user data folders to store browser data, such as cookies, permissions, and cached resources. WebView2 控件的每个实例都与一个用户数据文件夹关联。Each instance of a WebView2 control is associated with a user data folder. 每个用户数据文件夹对于用户都是唯一的。Each user data folder is unique to a user.

最佳做法Best Practices

用户数据文件夹由 WebView2 自动创建。User data folders are created automatically by WebView2. WebView2 开发人员控制用户数据文件夹的生命周期。WebView2 developers control the lifetime of the user data folder. 如果应用程序重新使用来自应用程序会话的用户数据,请考虑保存用户数据文件夹,否则可以将其删除。If your application re-uses user data from application sessions, consider saving the user data folders, otherwise you may delete them. 在决定如何管理用户数据文件夹时,请考虑以下方案:Consider the following scenarios when deciding how to manage your user data folders:

  • 如果同一用户反复使用您的应用程序,并且应用程序的 Web 内容依赖于用户的数据,请保存用户数据文件夹。If the same user uses your application repeatedly, and the web content of the application relies on the user's data, save the user data folder. 如果多个用户重复使用您的应用程序,请为每个新用户创建一个新的用户数据文件夹,并保存每个用户的用户数据文件夹。If multiple users use your application repeatedly, create a new user data folder for each new user, and save the user data folder of each user.
  • 如果应用程序没有重复用户,请为每个用户创建一个新的用户数据文件夹,并删除以前的用户数据文件夹。If your application does not have repeat users, create a new user data folder for each user, and delete the previous user data folder.

创建用户数据文件夹Create user data folders

若要指定用户数据文件夹的位置,在调用 userDataFolder ICoreWebView2Environment \ (Win32) 或 CoreWebView2Environment \ (.NET) 时包括 参数。To specify the location of the user data folder, include the userDataFolder parameter when calling ICoreWebView2Environment (Win32) or CoreWebView2Environment (.NET). 创建后,WebView2 控件中的浏览器数据存储在 的子文件夹内 userDataFolderAfter creation, browser data from your WebView2 control is stored in a subfolder of userDataFolder. 如果 userDataFolder 未指定,WebView2 将按如下方式在默认位置创建用户数据文件夹:When userDataFolder is not specified, WebView2 creates user data folders at default locations as follows:

  • 对于打包的 Windows 应用商店应用,默认用户文件夹是程序包文件夹中 ApplicationData\LocalFolder 的子文件夹。For packaged Windows Store apps, the default user folder is the ApplicationData\LocalFolder subfolder in the package's folder.
  • 对于现有桌面应用,默认用户数据文件夹是应用程序 + 的 exe 路径 .WebView2For existing desktop apps, the default user data folder is the exe path of your application + .WebView2. 我们建议你指定用户数据文件夹,并且你在存储所有其他应用数据的同一文件夹中创建它,而不是使用默认值。Instead of using the default, we recommend that you specify a user data folder, and that you create it in the same folder where all other app data is stored.

删除用户数据文件夹Delete user data folders

应用程序可能需要删除用户数据文件夹:Your application may need to delete user data folders:

  • 卸载应用时。When uninstalling your app. 如果要卸载打包的 Windows 应用商店应用,Windows 将自动删除用户数据文件夹。If you are uninstalling packaged Windows Store apps, Windows deletes user data folders automatically.
  • 清除所有浏览数据历史记录。To clean up all browsing data history.
  • 从数据损坏中恢复。To recover from data corruption.
  • 删除以前的会话数据。To remove previous session data.

备注

关闭 WebView2 应用程序后,用户数据文件夹中的文件可能仍在使用中。Files in user data folders may still be in use after the WebView2 application is closed. 在这种情况下,请等待浏览器进程及所有子进程退出,然后再删除该文件夹。In this situation, wait for the browser process and all child processes to exit before deleting the folder. 可以使用 WebView2 的 属性检索浏览器进程的进程 BrowserProcessId ID。You may retrieve the process id of the browser process using the BrowserProcessId property of the WebView2.

共享用户数据文件夹Share user data folders

WebView2 控件可以共享相同的用户数据文件夹,以:WebView2 controls may share the same user data folders to:

  • 通过运行在 一个浏览器进程中优化系统资源。Optimize system resources by running in one browser process.
  • 共享浏览器历史记录和缓存的资源。Share browser history and cached resources.

共享用户数据文件夹时,请考虑以下事项:Consider the following when sharing user data folders:

  1. 当重新创建 WebView2 控件以使用 add_NewBrowserVersionAvailable \ (Win32) 或 NewBrowserVersionAvailable \ (.NET) 事件更新浏览器版本时,请确保浏览器进程退出并关闭共享相同用户数据文件夹的 WebView2 控件。When re-creating WebView2 controls to update browser versions using add_NewBrowserVersionAvailable (Win32) or NewBrowserVersionAvailable (.NET) events, ensure browser processes exit and close WebView2 controls that share the same user data folder. 若要检索浏览器进程的进程 ID,请使用 BrowserProcessId WebView2 控件的 属性。To retrieve the process id of the browser process, use the BrowserProcessId property of the WebView2 control.
  2. 共享相同用户数据文件夹的 WebView2 控件必须对 ICoreWebView2Environment \ (Win32) 或 CoreWebView2Environment \ (.NET) 使用相同的选项。WebView2 controls that share the same user data folder must use the same options for ICoreWebView2Environment (Win32) or CoreWebView2Environment (.NET). 如果没有,WebView2 的创建将失败 HRESULT_FROM_WIN32(ERROR_INVALID_STATE)If not, the WebView2 creation will fail with HRESULT_FROM_WIN32(ERROR_INVALID_STATE).

若要隔离应用程序的不同部分或不需要在 WebView2 控件之间共享数据,可以选择使用不同的用户数据文件夹。To isolate different parts of your application or when sharing data between WebView2 controls is not needed, you may choose to use different user data folders. 例如,应用程序可能由两个 WebView2 控件组成,一个控件用于显示广告,另一个控件用于显示应用程序内容。For example, an application may consist of two WebView2 controls, one for displaying an advertisement and the other for displaying application content. 在此方案中,开发人员可以选择针对每个 WebView2 控件使用不同的用户数据文件夹。In this scenario, developers may opt to use different user data folders for each WebView2 control.

备注

每个 WebView2 浏览器进程会占用额外的内存和磁盘空间。Each WebView2 browser process consumes additional memory and disk space. 因此,我们建议不要同时运行包含过多不同用户数据文件夹的 WebView2。Therefore, we recommend not running WebView2s with too many different user data folders at the same time.