|
| 1 | + |
| 2 | +Title |
| 3 | +=== |
| 4 | +UserDataFolder API |
| 5 | + |
| 6 | +# Background |
| 7 | +You may have a need to cleanup or possibly add to the data being |
| 8 | +stored in the WebView2 user data directory. This directory can be either |
| 9 | +configured by you during creation of the webview2 control or calculated |
| 10 | +at runtime by the control if it wasn't set during creation. |
| 11 | + |
| 12 | +The current WebView2 code defaults the user data folder to the directory that |
| 13 | + contains the hosting application's executable. This has resulted in issues |
| 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. |
| 17 | + |
| 18 | +With the current implementation it is possible for you to have built |
| 19 | +logic into your application with an assumption where the User Data is |
| 20 | +located. WebView2 changing that default can then result in a code error or |
| 21 | +possible crash. |
| 22 | + |
| 23 | +This api will provide you a way to get the directory that is |
| 24 | +currently being used by the WebView for storage of the user data. |
| 25 | + |
| 26 | +Using this returned directory is safer for the long run so that if WebView2 |
| 27 | +changes the underlying path default logic the application will be able to adapt |
| 28 | +automatically. |
| 29 | + |
| 30 | +# Description |
| 31 | + |
| 32 | +Returns the user data folder that all CoreWebView2's created from this |
| 33 | +environment are using. |
| 34 | +This could be either the value passed in by you when creating the |
| 35 | +environment object or the calculated one for default handling. It will |
| 36 | +always be an absolute path. |
| 37 | + |
| 38 | + |
| 39 | +# Examples |
| 40 | + |
| 41 | +```cpp |
| 42 | +HRESULT UserDataFolder() |
| 43 | +{ |
| 44 | + Microsoft::WRL::ComPtr<ICoreWebView2Environment6> WebViewEnvironment6; |
| 45 | + m_webViewEnvironment->QueryInterface(IID_PPV_ARGS(&WebViewEnvironment6)); |
| 46 | + |
| 47 | + if (WebViewEnvironment6 != NULLPTR) |
| 48 | + { |
| 49 | + // get the current user data folder from the webview environment object |
| 50 | + wil::unique_cotaskmem_string userDataFolder; |
| 51 | + WebViewEnvironment6->get_UserDataFolder(&userDataFolder); |
| 52 | + |
| 53 | + // using the folder |
| 54 | + WriteAppLog(userDataFolder.get(), "Logging information"); |
| 55 | + } |
| 56 | +}``` |
| 57 | + |
| 58 | +```c# |
| 59 | + |
| 60 | + /// Webview creation and setup of completion handler are out of scope of sample |
| 61 | + void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e) |
| 62 | + { |
| 63 | + if (e.IsSuccess) |
| 64 | + { |
| 65 | + // Get the current user data folder |
| 66 | + String userDataFolder = webView.CoreWebView2.Environment.UserDataFolder(); |
| 67 | + |
| 68 | + // using the folder |
| 69 | + WriteAppLog(userDataFolder, "Logging information"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +``` |
| 74 | +
|
| 75 | +# API Details |
| 76 | +```c# (but really MIDL3) |
| 77 | +/// This interface is an extension of the ICoreWebView2Environment. An object |
| 78 | +/// implementing the ICoreWebView2Environment6 interface will also |
| 79 | +/// implement ICoreWebView2Environment. |
| 80 | +[ |
| 81 | + uuid(083CB0D7-E464-4108-807E-80AE4EAA3B28), object, |
| 82 | + pointer_default(unique) |
| 83 | +] interface ICoreWebView2Environment6 : ICoreWebView2Environment5 { |
| 84 | + /// Returns the user data folder that all CoreWebView2's created from this |
| 85 | + /// environment are using. |
| 86 | + /// This could be either the value passed in by the developer when creating the |
| 87 | + /// environment object or the calculated one for default handling. And will |
| 88 | + /// always be an absolute path. |
| 89 | + /// |
| 90 | + /// \snippet AppWindow.cpp GetUserDataFolder |
| 91 | +
|
| 92 | + [propget] HRESULT UserDataFolder([ out, retval ] LPWSTR * value); |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +```c# (but really MIDL3) |
| 97 | +namespace Microsoft.Web.WebView2.Core |
| 98 | +{ |
| 99 | + runtimeclass CoreWebView2Environment |
| 100 | + { |
| 101 | + [interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Environment6")] |
| 102 | + { |
| 103 | + // ICoreWebView2ExperimentalEnvironment5 members |
| 104 | + String UserDataFolder { get; }; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
0 commit comments