最近作了一個很小的功能,在網頁上面打開應用程序,用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>
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