@@ -6,13 +6,14 @@ UserDataFolder API
66# Background
77You may have a need to cleanup or possibly add to the data being
88stored in the WebView2 user data directory. This directory can be either
9- configured by the you during creation of the webview2 control or calculated
9+ configured by you during creation of the webview2 control or calculated
1010at runtime by the control if it wasn't set during creation.
1111
1212The current WebView2 code is designed to use a the directory that the hosting
1313application is running in as the default location. This has resulted in issues
14- if running from a secure location (IE the program files tree). Work is in process
15- to change that default and will be forthcoming in later changes
14+ if running from a folder for which the WebView2 process doesn't have write access
15+ such as a Program Files folder. Work is in progress to change that default and
16+ will be forthcoming in later changes.
1617
1718With the current guidance it is possible for you to have built
1819logic into your application with an assumption where the User Data is
@@ -31,7 +32,7 @@ automatically.
3132Returns the user data folder that all CoreWebView2's created from this
3233environment are using.
3334This could be either the value passed in by you when creating the
34- environment object or the calculated one for default handling. And it will
35+ environment object or the calculated one for default handling. It will
3536always be an absolute path.
3637
3738
@@ -40,27 +41,13 @@ always be an absolute path.
4041``` cpp
4142HRESULT UserDataFolder ()
4243{
43- base::ScopedTempDir temp_dir;
44+ // get the current user data folder from the webview environment object
45+ wil::unique_cotaskmem_string userDataFolder;
46+ m_webViewEnvironment->get_UserDataFolder(&userDataFolder);
4447
45- CreateCoreWebView2EnvironmentWithOptions (
46- L".", temp_dir.GetPath().value().c_str(), nullptr,
47- Microsoft::WRL::Callback<
48- ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>
49- (
50- [&temp_dir, &environment_created]
51- (HRESULT result, ICoreWebView2Environment* webview_environment) ->
52- HRESULT {
53-
54- ScopedCoMemString environmentUserDataFolder;
55- webview_environment->get_UserDataFolder(
56- &environmentUserDataFolder);
57-
58- return S_OK;
59- }).Get());
60-
61- return S_OK;_
62- }
63- ```
48+ // using the folder
49+ WriteAppLog (userDataFolder.get(), "Logging information");
50+ }```
6451
6552```c#
6653
@@ -69,7 +56,11 @@ HRESULT UserDataFolder()
6956 {
7057 if (e.IsSuccess)
7158 {
59+ // Get the current user data folder
7260 String userDataFolder = webView.CoreWebView2.Environment->UserDataFolder();
61+
62+ // using the folder
63+ WriteAppLog(userDataFolder, "Logging information");
7364 }
7465 }
7566
@@ -95,3 +86,11 @@ HRESULT UserDataFolder()
9586 [propget] HRESULT UserDataFolder([ out, retval ] LPWSTR * value);
9687}
9788```
89+
90+ ``` c# (but really MIDL3)
91+ [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2Environment6" )]
92+ {
93+ // ICoreWebView2ExperimentalEnvironment5 members
94+ String UserDataFolder { get ; };
95+ }
96+ ```
0 commit comments