CWebView::GetUserDataFolder Method  
 
INT GetUserDataFolder(
  LPTSTR lpszDataFolder,  
  INT nMaxLength,  
);

The GetUserDataFolder method returns the full path to the user data folder for the current session.

Parameters

lpszDataFolder
A null-terminated string buffer which will contain the path to user data folder for the session. If this parameter is NULL, the string will not be copied and the method will return the length of the path, not including the terminating null character.
nMaxLength
An integer which specifies the maximum number of characters which can be copied into the string buffer. If the lpszDataFolder parameter is NULL, this value should be zero.

Return Value

If the method succeeds, the return value is the number of characters in the User-Agent string, not including the terminating null character. If the method fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The User Data Folder (UDF) is the WebView2 profile directory that Chromium uses to persist browsing state for this instance. It contains the browser cache, cookies, localStorage/IndexedDB data, service worker state, visited link history, site permissions, and other profile data. Each distinct folder path corresponds to a distinct browser profile. Multiple WebView2 instances that point to the same folder will share the same profile (and therefore share cookies, storage, and authentication state). Using different folder paths isolates profiles.

The user data folder is chosen when the WebView instance is created or attached via Attach or Create and cannot be changed afterwards. If your application needs to switch profiles (for example, to use a different folder), you must destroy the current instance and create a new one with the desired folder.

If your application does not specify a folder or uses an empty string as the folder path, the library creates a unique per-session folder under the current user's local profile. This is an ephemeral profile that is not intended to be reused across browser instances. By default, this temporary folder is removed when the instance is detached; if the process terminates unexpectedly, the folder may remain and can be safely deleted later.

In contrast, specifying an explicit, persistent folder (for example, a subdirectory under %LocalAppData%\WebView\YourApp) gives you a shared profile that persists across sessions and can be reused by multiple WebView2 instances to enable single sign-on, shared cookies, and faster subsequent navigations due to caching. Use this approach when you want consistent, long-lived browser state.

The reported folder path is always an absolute, normalized local path. Environment variables in the path you supply at creation time are expanded; forward slashes are normalized to backslashes and relative names are resolved beneath the current user's local application data folder. The library rejects invalid or unsafe locations (e.g.: device names, system directories, UNC or extended path prefixes, or root drive targets) and will create intermediate directories as needed for valid paths. For multithreaded applications, you should use explicit absolute paths rather than relying on the process current directory.

Important: The user data folder is managed internally by the WebView2 runtime, and your application should never attempt to modify, move, or delete any of the files it contains while a browser instance is active. The contents are in use by the rendering engine and interfering with them can result in data corruption or unexpected behavior. If you need to remove or relocate a profile, ensure that all browser instances using that folder have been completely closed. If you want to delete certain data, such as cookies or the browser history, use the methods provided specifically for that purpose.

You can call GetUserDataFolder at any time after the instance has been created to discover which profile directory is in use. Passing lpszDataFolder as NULL returns the required buffer length so you can allocate a buffer of the appropriate size; the returned length does not include the terminating null character.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also