1

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?

Myronaz
  • 183
  • 1
  • 1
  • 10

1 Answers1

4

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:

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • Did you read my question? I'm using VB.NET (not C#) and I already mentioned that the `CoreWebView2EnvironmentOptions` doesn't seem to exist in the namespace. – Myronaz Mar 31 '22 at 22:59
  • @Myronaz: Sorry, I posted the wrong one. I've updated the post with the one for VB.NET. – Tu deschizi eu inchid Mar 31 '22 at 23:05
  • No worries, I didn't see your import statements so I messed up as well and submitted an edit earlier so just discard it or ignore it. Ugh... so embarrassing. By the way, the code for the Form produces a warning that the async task returned from the function will be dropped, thus causing exceptions to be ignored. – Myronaz Apr 01 '22 at 00:27
  • @Myronaz: I don't receive any warnings when I compile. I've compiled the code above in VS 2019 WinForms (.NET Framework) and VS 2022 WinForms .NET 6. If you are still having issues, add the code that you're using to your OP as an update. Also, include any errors/warnings. – Tu deschizi eu inchid Apr 01 '22 at 02:14
  • The following may be helpful: https://stackoverflow.com/questions/70625883/webview2-vb-net-cant-get-webpage-to-load/70645764#70645764 – Tu deschizi eu inchid Apr 01 '22 at 02:28
  • Thank you! I'm not sure how but VS2022 changed my Form's `Load()` event sub into a function that gets the task, which gets the task and drops it when finished. This is why I was getting the error. I'll mark this as solved. – Myronaz Apr 01 '22 at 14:08