C# CefSharp如何在Winforms应用程序中使用
最近做了一个很小的功能,在网页上面打开应用程序,用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>
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应用程序中使用的更多相关文章
- 转:如何在32位程序中突破地址空间4G的限制
//如何在32位程序中突破地址空间4G的限制 //首先要获得内存中锁定页的权限 #define _WIN32_WINNT 0x0501 //xp系统 #include <windows.h> ...
- 如何在Android应用程序中使用传感器模拟器SensorSimulator
原文地址; 如何在Android应用程序中使用传感器模拟器 - 移动平台应用软件开发技术 - 博客频道 - CSDN.NET http://blog.csdn.net/pku_android/arti ...
- [zz]如何在C语言程序中处理汉字
学习过C语言的人也许有时会遇到这样一个问题:如何用变量存储汉字以及对这些变量进行操作.目前许多C语言参考书中都没涉及到这个问题,程序中多为处理英文变量和英文字符串,涉及到汉字的情况也大都是在print ...
- 如何在Silverlight应用程序中获取ASP.NET页面参数
asp.net Silverlight应用程序中获取载体aspx页面参数 有时候SL应用中需要使用由aspx页面中传递过来的参数值,此时通常有两种方法获取 1. 使用InitParameters属性, ...
- 如何在WPF应用程序中使用视频处理控件TVideoGrabber
要在WPF 中使用 TVideoGrabber 组件,需要像下面的方法来使用 VS.NET(DLL) 版本的组件: ——复制TVideoGrabber_x.x.x.x_x86.dll到c:/windo ...
- 如何在Android应用程序中使用传感器(OpenIntents开源组织SensorSimulator项目)
原文地址http://blog.sina.com.cn/s/blog_621c16b101013ygl.html OpenIntents项目和可用资源介绍 [1]. 项目介绍:OpenInten ...
- [Prism框架实用分享]如何在Prism应用程序中使用日志
前言 在Prism中有关日志的命名空间: Microsoft.Practices.Prism.Logging 在Prism中,你可以使用Prism自带的Logger(比如TextLogger等),也可 ...
- 示例 - 如何在Console应用程序中应用SpiderStudio生成的DLL?
以前面生成的XML/JSON互转DLL为例, 我们写一个Console Appliction来做这件事情, 步骤如下: 1. 创建Console Project 2. 引入www.utilities_ ...
- 如何在Qt 4程序中优化布局结构(表格讲解,很清楚)
原文地址:http://blog.csdn.net/qter_wd007/archive/2010/03/13/5377882.aspx 在迄今为止讲到每一个例子中,我们只是简单的把窗口部件放置到某个 ...
随机推荐
- Zabbix 3.0 配置企业微信报警(注册---测试)
一.申请企业微信 1.登录企业微信官网,点击企业注册 二.配置企业微信 1.邀请管理员使用企业微信,如果有多个人直接添加新成员 2.管理员收到邀请,下载手机版企业微信,使用微信号登陆即可 3.创建应用 ...
- 将博客转成pdf
前些天无意间看到了“birdben”的博客,写的比较详细,但是最新的文章更新时间是“2017-05-07”,时间很是久远,本打算有时间认真学习一下博主所写的文章,但是担心网站会因为某些原因停止服务,于 ...
- Mysql update多表联合更新
下面我建两个表,并执行一系列sql语句,仔细观察sql执行后表中数据的变化,很容易就能理解多表联合更新的用法 student表 ...
- 解决IntelliJ IDEA 控制台输出中文乱码问题
解决IntelliJ IDEA 控制台输出中文乱码问题 问题描述:如图,控制台输出的字符,乱码 解决方案 第一步:修改intellij idea配置文件: 找到Intellij idea安装目录,bi ...
- spring mvc 处理pojo传递对象时该对象继承父类的属性在网络接收端接收该属性值总是null,why?
//=========================== 情形一: ===============================//在网络上传递User1类对象时info属性值在网络的另一端能够接 ...
- 网站登录注册-Session 和token的总结
1.为什么要使用session 因为http本身是无状态协议,无法确定你的本次请求和上次请求是不是你发送的.如果要进行类似论坛登陆相关的操作,就实现不了了. 2.Session 生成方式 浏览器第一次 ...
- 一个 frameset 框架
<frameset border="0" framespacing="0" rows="45,*" frameborder=" ...
- vscode 前端常用插件推荐
1. vscode 简介vscode是微软开发的的一款代码编辑器,就如官网上说的一样,vscode重新定义(redefined)了代码编辑器.当前市面上常用的轻型代码编辑器主要是:sublime,n ...
- RSA非对称 私钥加密
RSA生成公钥和私钥对 /// <summary> /// RSA生成公钥和私钥 /// </summary> /// <returns></returns& ...
- JDBC简单增删改查实现(单表)
0.准备工作 开发工具: MySQL数据库, intelliJ IDEA2017. 准备jar包: mysql-connector-java-5.1.28-bin.jar(其他均可) 1. 数据库数据 ...