How to use C # CefSharp in Winforms application

Recently did a small feature, open the application on the page above, use the debug vs, you can open normal applications, but can not be arranged to run the application iis above, I Baidu, the said iis permissions issue, I in accordance with reason to do it, how frustrating is not OK. Finally boss gave two solutions, a first, disposable b / s change c / s, a second, with the CefSharp b / s embedded into the site. b / s website has done a token gesture, it will use the CefSharp.

Here are the steps to use CefSharp of:

 1. Create a basic Winforms application, and add CefSharp use NuGet package.

    Before you create, make sure your computer is installed: CefSharp 45.0 and later need to install VC 2013 Redistributable Package x86, earlier versions need VC 2012 Redistributable Package x86. If not, the following error will be reported.

  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.

    Usually install the latest version of CefSharp, recommendations completely closed VS, and then turn it on again (This will ensure that your quote display, and a complete intellisense), or you may cause an error: Can not find the type or namespace name "Cefsharp" ( Are you missing a using directive or an assembly reference?)

2 Change configuration platform (x86, x64, or AnyCPU)

   I used CefSharp version is 51 or more, so to modify the configuration:

   First, search your-project-name.csproj file, and in the first <PropertyGroup> node added:

     <CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>

   Then modify the app.config file:

     <runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <probing privatePath="x86"/>
    </assemblyBinding>
 </runtime>
3 cefsharp has been installed and configured, you can now write code.
  This is winfrom code
Copy the code
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();
        }
    }
}
Copy the code
This is the application's code execution
Copy the code
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);
        }

       
    }
}
Copy the code

Recall the last web page

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

 Reference URL: http: //www.libs.org.cn/index.php m = content & c = index & a = show & catid = 90 & id = 129? 

 

Source: https://www.cnblogs.com/shimiyan/p/6932073.html

Recently did a small feature, open the application on the page above, use the debug vs, you can open normal applications, but can not be arranged to run the application iis above, I Baidu, the said iis permissions issue, I in accordance with reason to do it, how frustrating is not OK. Finally boss gave two solutions, a first, disposable b / s change c / s, a second, with the CefSharp b / s embedded into the site. b / s website has done a token gesture, it will use the CefSharp.

Here are the steps to use CefSharp of:

 1. Create a basic Winforms application, and add CefSharp use NuGet package.

    Before you create, make sure your computer is installed: CefSharp 45.0 and later need to install VC 2013 Redistributable Package x86, earlier versions need VC 2012 Redistributable Package x86. If not, the following error will be reported.

  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.

    Usually install the latest version of CefSharp, recommendations completely closed VS, and then turn it on again (This will ensure that your quote display, and a complete intellisense), or you may cause an error: Can not find the type or namespace name "Cefsharp" ( Are you missing a using directive or an assembly reference?)

2 Change configuration platform (x86, x64, or AnyCPU)

   I used CefSharp version is 51 or more, so to modify the configuration:

   First, search your-project-name.csproj file, and in the first <PropertyGroup> node added:

     <CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>

   Then modify the app.config file:

     <runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <probing privatePath="x86"/>
    </assemblyBinding>
 </runtime>
3 cefsharp has been installed and configured, you can now write code.
  This is winfrom code
Copy the code
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();
        }
    }
}
Copy the code
This is the application's code execution
Copy the code
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);
        }

       
    }
}
Copy the code

Recall the last web page

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

 Reference URL: http: //www.libs.org.cn/index.php m = content & c = index & a = show & catid = 90 & id = 129? 

Guess you like

Origin www.cnblogs.com/mq0036/p/11059648.html