如何在 WinForms VB.NET 应用程序中设置 WebView2 用户数据文件夹?
How do you set the WebView2 User Data Folder in a WinForms VB.NET app?
我需要将用户数据文件夹设置为我选择的自定义路径,以便对其进行控制。 In Microsoft's documentation,说明可以在环境初始化前使用CoreWebView2EnvironmentClass设置该选项。
但这是针对 C# 的,而不是 WinForms VB.NET (.Net Framework)。命名空间中甚至没有 CoreWebView2Environment
,只有 CoreWebView2.Environment
没有相同的功能,但似乎具有 returns 路径为只读的功能字符串。
我找不到关于此 class 的任何文档。有谁知道这是否可行?
要显式初始化 CoreWebView2
,请尝试以下操作:
添加以下导入语句:
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.0.1054.31", userDataFolder, options)
cwv2Environment = Await CoreWebView2Environment.CreateAsync(Nothing, userDataFolder, options)
'initialize
Await WebView21.EnsureCoreWebView2Async(cwv2Environment)
End Function
注意:如果希望显式初始化CoreWebView2
,则必须在为[设置Source
属性之前完成=19=]控制.
用法:
Await InitializeCoreWebView2Async(Path.Combine("C:\Temp", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name))
如果您在表单中调用它(例如:Form1.vb),那么您将执行以下操作:
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
注意:CoreWebView2CreationProperties 可能也有兴趣。
资源:
我需要将用户数据文件夹设置为我选择的自定义路径,以便对其进行控制。 In Microsoft's documentation,说明可以在环境初始化前使用CoreWebView2EnvironmentClass设置该选项。
但这是针对 C# 的,而不是 WinForms VB.NET (.Net Framework)。命名空间中甚至没有 CoreWebView2Environment
,只有 CoreWebView2.Environment
没有相同的功能,但似乎具有 returns 路径为只读的功能字符串。
我找不到关于此 class 的任何文档。有谁知道这是否可行?
要显式初始化 CoreWebView2
,请尝试以下操作:
添加以下导入语句:
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.0.1054.31", userDataFolder, options)
cwv2Environment = Await CoreWebView2Environment.CreateAsync(Nothing, userDataFolder, options)
'initialize
Await WebView21.EnsureCoreWebView2Async(cwv2Environment)
End Function
注意:如果希望显式初始化CoreWebView2
,则必须在为[设置Source
属性之前完成=19=]控制.
用法:
Await InitializeCoreWebView2Async(Path.Combine("C:\Temp", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name))
如果您在表单中调用它(例如:Form1.vb),那么您将执行以下操作:
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
注意:CoreWebView2CreationProperties 可能也有兴趣。
资源: