windowForm程序的webView2错误 System.IO.FileNotFoundException: 系统找不到指定的文件。(异常来自HRESULT:0x80070002)
  zdfkqDvyzOHI 2024年02月19日 103 0

最近开发公司的一个项目,要求打包在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

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();
        }

如果还是出现了错误,那么把项目打包文件的Release中的xx.WebView2文件夹删除掉,重新运行。

这个目录是webVew2插件生成的文件,如果直接拷贝到其他的电脑上运行就会出现兼容性的问题。

 

 完结。

 

bug处理日志记录一下

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年02月19日 0

暂无评论

推荐阅读
  NPQODODLqddb   2024年05月17日   67   0   0 .NET
  mVIyUuLhKsxa   2024年05月17日   52   0   0 .NET
  XkHDHG7Y62UM   2024年05月17日   43   0   0 .NET
  f18CFixvrKz8   2024年05月18日   83   0   0 .NET
  rBgzkhl6abbw   2024年05月18日   75   0   0 .NET
  MYrYhn3ObP4r   2024年05月17日   40   0   0 .NET
  S34pIcuyyIVd   2024年05月17日   57   0   0 .NET
  gKJ2xtp6I8Y7   2024年05月17日   50   0   0 .NET
  MYrYhn3ObP4r   2024年05月17日   39   0   0 .NET
zdfkqDvyzOHI