C# CefSharp如何在Winforms应用程序中使用

最近作了一个很小的功能,在网页上面打开应用程序,用vs的debug调试,能够正常打开应用程序,可布置到iis上面却没法运行应用程序,吾百度之,说是iis权限问题,吾依理作之,可怎么折腾也不行。最后boss给了两种方案,第一,弃b/s改c/s,第二,用CefSharp把b/s网站嵌进去。b/s网站已作完,弃之惋惜,吾便用了CefSharp。javascript

如下是使用CefSharp的步骤:php

 1.建立一个基本的Winforms应用程序,并添加CefSharp使用NuGet包。html

    在建立以前,请确保计算机已安装:CefSharp 45.0及更高版本须要安装VC 2013 Redistributable Package x86,早期版本须要VC 2012 Redistributable Package x86。若是未安装,会报如下错误。java

  An unhandled exception of type 'System.IO.FileNotFoundException' occurred in browser.exe Additional information: Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies.web

    一般安装最新版本的CefSharp,建议彻底关闭VS,而后从新打开(这样能够确保您的引用显示,并有完整的intellisense),不然你可能会发生错误:找不到类型或命名空间名称“Cefsharp”(是否缺乏using指令或程序集引用?)chrome

2 更改平台配置(x86,x64或AnyCPU)c#

   吾用的CefSharp版本是51以上,因此要修改配置:app

   首先,搜索your-project-name.csproj文件,并在第一个 <PropertyGroup>的节点添加:ide

     <CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>post

   而后修改app.config文件:

     <runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <probing privatePath="x86"/>
    </assemblyBinding>
 </runtime>
3 cefsharp已经安装并配置完成,如今就能够写代码了.
  这是winfrom的代码
复制代码
using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CefSharp; using CefSharp.WinForms; using System.Configuration; namespace VR.Winfrom { public partial class Form1 : Form { public ChromiumWebBrowser chromeBrowser; public Form1() { InitializeComponent(); this.WindowState = FormWindowState.Maximized; InitializeChromium(); // Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3
            chromeBrowser.RegisterJsObject("formProcess", new FormProcess(chromeBrowser, this)); } public void InitializeChromium() { CefSettings settings = new CefSettings(); // Initialize cef with the provided settings
 Cef.Initialize(settings); // Create a browser component
            string url = ConfigurationManager.AppSettings["Url"]; chromeBrowser = new ChromiumWebBrowser(url); // Add it to the form and fill it to the form window.
            this.Controls.Add(chromeBrowser); chromeBrowser.Dock = DockStyle.Fill; // Allow the use of local resources in the browser
            BrowserSettings browserSettings = new BrowserSettings(); browserSettings.FileAccessFromFileUrls = CefState.Enabled; browserSettings.UniversalAccessFromFileUrls = CefState.Enabled; browserSettings.WebSecurity = CefState.Enabled; chromeBrowser.BrowserSettings = browserSettings; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Cef.Shutdown(); } } }
复制代码
这是执行应用程序的代码
复制代码
using System.Diagnostics;
using CefSharp.WinForms;
using VR.DAL;
using System;

namespace VR.Winfrom
{
   public class FormProcess
    {
        // Declare a local instance of chromium and the main form in order to execute things from here in the main thread
        private static ChromiumWebBrowser _instanceBrowser = null;
        // The form class needs to be changed according to yours
        private static Form1 _instanceMainForm = null;
        
        public FormProcess(ChromiumWebBrowser originalBrowser, Form1 mainForm)
        {
            _instanceBrowser = originalBrowser;
            _instanceMainForm = mainForm;
        }

        public void opencmd(string filePath)
        {
              
                string file = @"" + filePath;
                ProcessStartInfo start = new ProcessStartInfo(file);
                Process.Start(start);
        }

       
    }
}
复制代码

最后web页面的调用

<button class="btn btn-primary" onclick="FormProcess.opencmd('C://Program Files (x86)//Google//Chrome//Application//chrome.exe');">Open</button>

 参考网址:http://www.libs.org.cn/index.php?m=content&c=index&a=show&catid=90&id=129 

 

出处:https://www.cnblogs.com/shimiyan/p/6932073.html