最近做了一个很小的功能,在网页上面打开应用程序,用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. 【MongoDB学习之五】Java中使用MongoDB

    环境 MongoDB 3.0 CentOS6.5_x64 相关jar:mongo-java-driver-3.0.0.jar package com.mongodb; import java.util ...

  2. ubantu系统安装ssh

    ssh连接ubantu系统 描述:新安装的ubantu系统,ssh连接发现22端口拒绝,登陆服务器发现没有ssh 1.安装ssh服务 apt-get install openssh-server 报错 ...

  3. windows 系统下安装docker

    一.介绍 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间 ...

  4. [数据结构 - 第3章] 线性表之单链表(C++实现)

    一.类定义 单链表类的定义如下: #ifndef SIGNALLIST_H #define SIGNALLIST_H typedef int ElemType; /* "ElemType类型 ...

  5. xmlrpc与jsonrpc

    RPC是Remote Procedure Call的缩写,翻译成中文就是远程过程调用,是一种在本地的机器上调用远端机器上的一个过程(方法)的技术,这个过程也被大家称为“分布式计算”,是为了提高各个分立 ...

  6. jquery关于on click事件的理解

    jquery关于on click事件的理解 <pre><a style="min-width:60px; margin-left:6px;" wenzhangid ...

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

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

  8. .whl文件安装cuda10.0版本的pytorch1.3.0+torchvision0.4.1

    $ python3 -m venv env3$ source env3/bin/activate$ cd env3/share/python-wheels 在此找到对应的版本:https://down ...

  9. PHP中YUM的理解

    1. YUM是什么? 1)全称:Yellow dog Updater ,Modified. 2)百度简述:是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包 ...

  10. Python中的sync和wait函数的使用

    转自这篇博文,备忘: https://blog.csdn.net/Likianta/article/details/90123678 https://www.cnblogs.com/xinghun85 ...