How to set the user data folder for the new Edge browser WebView2 component.
In one of my applications I was replacing the old Internet Explorer component with the new WebView2 component, which uses the new Edge browser engine.
The replacement was easy, however the browser creates a user data folder. By default the location was in the same folder as my application. The problem is, that this folder is usually not writeable, since my application is installed to the program folder.
So I had to set the folder, here is my code:
private async void MarkdownControl_Load(object sender, EventArgs e) { await InitializeAsync(); } public async Task InitializeAsync() { string userDataFolder = Path.Combine(Path.GetTempPath(), "DA-HelpCreator"); webBrowserPreview.CreationProperties = new CoreWebView2CreationProperties { UserDataFolder = userDataFolder }; await webBrowserPreview.EnsureCoreWebView2Async(); }
Important is, that the last command uses „await“, so it can not be run from the components or applications constructor. That is why I am using the Load event. In my case I create the folder in the users temp folder.