翼度科技»论坛 编程开发 .net 查看内容

windowForm程序的webView2错误 System.IO.FileNotFoundException: 系统找不

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
最近开发公司的一个项目,要求打包在windows中执行的exe可执行文件
开始我想到的是使用windowsForm里面webView嵌套网页执行,
vs自带提供的WebBrowser的内核是ie7的,兼容性确实不好,后面使用Microsoft.Web.WebView2(通过NuGet安装)兼容性问题解决了。
在我的电脑上可以完整的运行,但是在同事的电脑上运行出现错误

 只告诉你系统找不到指定文件,没有说是什么文件
后面打印得出错误:
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/

 下载好后,放入到项目的Release目录和Debug目录
 
然后启动项目时,加入检查edge
  1. internal static class Program
  2.     {
  3.         /// <summary>
  4.         /// 应用程序的主入口点。
  5.         /// </summary>
  6.         [STAThread]
  7.         static void Main()
  8.         {
  9.             WebViewInint().Wait();
  10.             if (CheckWebView2().instanlled)
  11.             {
  12.                 Application.EnableVisualStyles();
  13.                 Application.SetCompatibleTextRenderingDefault(false);
  14.                 Application.Run(new Form1());
  15.             }
  16.             else
  17.             {
  18.                 //todo:未正确安装webview runtime处理
  19.                 MessageBox.Show("Edge webview2 runtime 未正确安装,即将退出程序!");
  20.             }
  21.         }
  22.         public static async Task WebViewInint()
  23.         {
  24.             var checkResuilt = CheckWebView2();
  25.             if (!checkResuilt.instanlled)
  26.             {
  27.                 await installView2().ConfigureAwait(false);
  28.             }
  29.         }
  30.         public static (bool instanlled, string version) CheckWebView2()
  31.         {
  32.             try
  33.             {
  34.                 var str = CoreWebView2Environment.GetAvailableBrowserVersionString();
  35.                 if (!string.IsNullOrWhiteSpace(str))
  36.                 {
  37.                     return (true, str);
  38.                 }
  39.             }
  40.             catch
  41.             {
  42.             }
  43.             return (false, null);
  44.         }
  45.         public static Task installView2()
  46.         {
  47.             return Task.Run(() =>
  48.             {
  49.                 using (Process process = new Process())
  50.                 {
  51.                     process.StartInfo.UseShellExecute = false;
  52.                     process.StartInfo.FileName = System.IO.Path.GetFullPath("MicrosoftEdgeWebview2Setup.exe");
  53.                     process.Start();
  54.                     process.WaitForExit();
  55.                 }
  56.             });
  57.         }
复制代码
 窗口加载代码:
  1. private async void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             var ipAddr = "https://www.baidu.com/";
  4.             var options = new CoreWebView2EnvironmentOptions("--autoplay-policy=no-user-gesture-required");
  5.             var environment = await CoreWebView2Environment.CreateAsync(null, null, options);
  6.             webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
  7.             webView.EnsureCoreWebView2Async();
  8.         }
复制代码
 完结。
 
bug处理日志记录一下
 

来源:https://www.cnblogs.com/yangWanSheng/p/17991214
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具