windowForm程序的webView2错误 System.IO.FileNotFoundException: 系统找不到指定的文件。(异常来自HRESULT:0x80070002)

  • windowForm程序的webView2错误 System.IO.FileNotFoundException: 系统找不到指定的文件。(异常来自HRESULT:0x80070002)已关闭评论
  • 68 次浏览
  • A+
所属分类:.NET技术
摘要

最近开发公司的一个项目,要求打包在windows中执行的exe可执行文件开始我想到的是使用windowsForm里面webView嵌套网页执行,

最近开发公司的一个项目,要求打包在windows中执行的exe可执行文件

开始我想到的是使用windowsForm里面webView嵌套网页执行,

vs自带提供的WebBrowser的内核是ie7的,兼容性确实不好,后面使用Microsoft.Web.WebView2(通过NuGet安装)兼容性问题解决了。

在我的电脑上可以完整的运行,但是在同事的电脑上运行出现错误

windowForm程序的webView2错误 System.IO.FileNotFoundException: 系统找不到指定的文件。(异常来自HRESULT:0x80070002)

 只告诉你系统找不到指定文件,没有说是什么文件

后面打印得出错误:

Microsoft.Web.WebView2.Core.WebView2RuntimeNotFoundException: Couldn't find a compatible Webview2 Runtime installation to host WebViews. ---> System.IO.FileNotFoundException: 系统找不到指定的文件。 (异常来自 HRESULT:0x80070002)

其实就是电脑上的Edge浏览器和你使用的webView中的版本不兼容,找不到该项目中webview2自己使用的runtime。

解决办法:

到官网中下载Edge更新插件: 地址: https://developer.microsoft.com/zh-cn/microsoft-edge/webview2/

windowForm程序的webView2错误 System.IO.FileNotFoundException: 系统找不到指定的文件。(异常来自HRESULT:0x80070002)

 下载好后,放入到项目的Release目录和Debug目录

 

然后启动项目时,加入检查edge

internal static class Program     {         /// <summary>         /// 应用程序的主入口点。         /// </summary>         [STAThread]         static void Main()         {             WebViewInint().Wait();             if (CheckWebView2().instanlled)             {                 Application.EnableVisualStyles();                 Application.SetCompatibleTextRenderingDefault(false);                 Application.Run(new Form1());             }             else             {                 //todo:未正确安装webview runtime处理                 MessageBox.Show("Edge webview2 runtime 未正确安装,即将退出程序!");             }         }          public static async Task WebViewInint()         {             var checkResuilt = CheckWebView2();              if (!checkResuilt.instanlled)             {                  await installView2().ConfigureAwait(false);             }           }         public static (bool instanlled, string version) CheckWebView2()         {             try             {                 var str = CoreWebView2Environment.GetAvailableBrowserVersionString();                 if (!string.IsNullOrWhiteSpace(str))                 {                     return (true, str);                 }             }             catch             {              }             return (false, null);         }          public static Task installView2()         {             return Task.Run(() =>             {                 using (Process process = new Process())                 {                     process.StartInfo.UseShellExecute = false;                     process.StartInfo.FileName = System.IO.Path.GetFullPath("MicrosoftEdgeWebview2Setup.exe");                     process.Start();                     process.WaitForExit();                  }              });         }

 窗口加载代码:

 private async void Form1_Load(object sender, EventArgs e)         {             var ipAddr = "https://www.baidu.com/";             var options = new CoreWebView2EnvironmentOptions("--autoplay-policy=no-user-gesture-required");             var environment = await CoreWebView2Environment.CreateAsync(null, null, options);             webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;             webView.EnsureCoreWebView2Async();         }

 完结。

 

bug处理日志记录一下