Home > Enterprise >  How do you set the WebView2 User Data Folder in a WinForms VB.NET app?
How do you set the WebView2 User Data Folder in a WinForms VB.NET app?

Time:04-01

I need to set the User Data Folder to a custom path of my choice in order to have control over it. In Microsoft's documentation, it is explained that the option can be set using the CoreWebView2Environment Class before the environment is initialized.

But this is for C#, and not WinForms VB.NET (.Net Framework). There is not even a CoreWebView2Environment in the namespace, there is only CoreWebView2.Environment which does not have the same functions, but does appear to have a function that returns the path as a read-only string.

I'm not able to find any documentation about this class. Does anyone know if this is even possible to do?

CodePudding user response:

To explicitly initialize CoreWebView2, try the following:

Add the following Imports statements:

  • Imports Microsoft.Web.WebView2.Core
  • Imports Microsoft.Web.WebView2
  • Imports System.IO

InitializeCoreWebView2Async:

Private Async Function InitializeCoreWebView2Async(Optional userDataFolder As String = "") As Task
    Dim options As CoreWebView2EnvironmentOptions = Nothing
    Dim cwv2Environment As CoreWebView2Environment = Nothing

    'it's recommended to create the userDataFolder in the same location
    'that your other application data is stored (ie: in a folder in %APPDATA%)
    'if not specified, we'll create a folder in %TEMP%
    If String.IsNullOrEmpty(userDataFolder) Then
        userDataFolder = Path.Combine(Path.GetTempPath(), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name)
    End If

    'create WebView2 Environment using the installed or specified WebView2 Runtime version.
    'cwv2Environment = Await CoreWebView2Environment.CreateAsync("C:\Program Files (x86)\Microsoft\Edge Dev\Application\1.0.1054.31", userDataFolder, options)
    cwv2Environment = Await CoreWebView2Environment.CreateAsync(Nothing, userDataFolder, options)

    'initialize
    Await WebView21.EnsureCoreWebView2Async(cwv2Environment)
End Function

Note: If one desires to explicitly initialize CoreWebView2, it must be done prior to setting the Source property for the WebView2 control.

Usage:

Await InitializeCoreWebView2Async(Path.Combine("C:\Temp", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name))

If you're calling this in a form (ex: Form1.vb), then you'd do the following:

Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    System.Diagnostics.Debug.WriteLine("MS Edge Version: " & CoreWebView2Environment.GetAvailableBrowserVersionString())

    'initialize 
    'Await InitializeCoreWebView2Async()
    Await InitializeCoreWebView2Async(Path.Combine("C:\Temp", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name))

    'ToDo: add desired code, such as navigating to a URL

End Sub

Note: CoreWebView2CreationProperties may be of interest as well.

Resources:

  • Related