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

以下是使用CefSharp的步骤:

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

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

  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.

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

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

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

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

<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>

然后修改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

C# CefSharp如何在Winforms应用程序中使用的更多相关文章

  1. 转:如何在32位程序中突破地址空间4G的限制

    //如何在32位程序中突破地址空间4G的限制 //首先要获得内存中锁定页的权限 #define _WIN32_WINNT 0x0501 //xp系统 #include <windows.h> ...

  2. 如何在Android应用程序中使用传感器模拟器SensorSimulator

    原文地址; 如何在Android应用程序中使用传感器模拟器 - 移动平台应用软件开发技术 - 博客频道 - CSDN.NET http://blog.csdn.net/pku_android/arti ...

  3. [zz]如何在C语言程序中处理汉字

    学习过C语言的人也许有时会遇到这样一个问题:如何用变量存储汉字以及对这些变量进行操作.目前许多C语言参考书中都没涉及到这个问题,程序中多为处理英文变量和英文字符串,涉及到汉字的情况也大都是在print ...

  4. 如何在Silverlight应用程序中获取ASP.NET页面参数

    asp.net Silverlight应用程序中获取载体aspx页面参数 有时候SL应用中需要使用由aspx页面中传递过来的参数值,此时通常有两种方法获取 1. 使用InitParameters属性, ...

  5. 如何在WPF应用程序中使用视频处理控件TVideoGrabber

    要在WPF 中使用 TVideoGrabber 组件,需要像下面的方法来使用 VS.NET(DLL) 版本的组件: ——复制TVideoGrabber_x.x.x.x_x86.dll到c:/windo ...

  6. 如何在Android应用程序中使用传感器(OpenIntents开源组织SensorSimulator项目)

    原文地址http://blog.sina.com.cn/s/blog_621c16b101013ygl.html OpenIntents项目和可用资源介绍 [1].    项目介绍:OpenInten ...

  7. [Prism框架实用分享]如何在Prism应用程序中使用日志

    前言 在Prism中有关日志的命名空间: Microsoft.Practices.Prism.Logging 在Prism中,你可以使用Prism自带的Logger(比如TextLogger等),也可 ...

  8. 示例 - 如何在Console应用程序中应用SpiderStudio生成的DLL?

    以前面生成的XML/JSON互转DLL为例, 我们写一个Console Appliction来做这件事情, 步骤如下: 1. 创建Console Project 2. 引入www.utilities_ ...

  9. 如何在Qt 4程序中优化布局结构(表格讲解,很清楚)

    原文地址:http://blog.csdn.net/qter_wd007/archive/2010/03/13/5377882.aspx 在迄今为止讲到每一个例子中,我们只是简单的把窗口部件放置到某个 ...

随机推荐

  1. sql server 2008 自动备份

    身份验证:包含Windows身份验证和 SQL Server身份验证,此处选择Windows 身份验证; 选择[管理]-->[维护计划]-->[维护计划向导] 必须启用代理服务(启动模式请 ...

  2. 修改mysql max_allowed_packet 配置

    1:当前默认配置 mysql> show VARIABLES like '%max_allowed_packet%';+--------------------------+---------- ...

  3. idea springboot启动报SLF4J:Failed to load class “org.slf4j.impl.StaticLoggerBinder”

    <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artif ...

  4. Django-12-auth认证组件

    1. 介绍 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能. Django作为一个完美主义者的终极框架,当然也会 ...

  5. python 之 面向对象 (异常处理)

    7.15 异常处理 1.什么是异常 异常是错误发生的信号,程序一旦出错,如果程序中还没有相应的处理机制,那么该错误就会产生一个异常抛出来,程序的运行也随之终止 2.一个异常分为三部分: 异常的追踪信息 ...

  6. 【转】python测试框架--doctest

    转自https://my.oschina.net/lionets/blog/268542 doctest 是一个 Python 发行版自带的标准模块.有两种模式可供使用. ##1 doctest 的概 ...

  7. golang 网络编程之如何正确关闭tcp连接以及管理它的生命周期

    欢迎访问我的个人网站获取更佳阅读排版 golang 网络编程之如何正确关闭tcp连接以及管理它的生命周期 | yoko blog (https://pengrl.com/p/47401/) 本篇文章部 ...

  8. Go基础编程实践(八)—— 系统编程

    捕捉信号 // 运行此程序,控制台将打印"Waiting for signal" // 按Ctrl + C 发送信号以关闭程序,将发生中断 // 随后控制台依次打印"Si ...

  9. Git diff (---和+++具体解释)(转)

    转自:https://blog.csdn.net/lovezbs/article/details/46492933

  10. ansible debugger 模块

    在搞TF(tungstenfabric)时遇到了一些错误,TF通过ansible playbook 来部署的.通常情况下遇到错误都是通过ansibale xxxx –vvv 来详细输出一下.出错的类型 ...