Search code examples
c#.netchromiumcefsharpchromium-embedded

Placing CefSharp dependencies into a specific directory


I am trying to create a .net forms application that uses cefsharp, but all of the cefsharp dependencies will be placed and used from a specific directory on the PC(let' s say C:\Chromium)

I have seen some entries but almost all of them are ancient, and using very old versions of cefsharp.

How can I achieve this with cefsharp 96.0.142?

I already tried

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <probing privatePath="libs"/>
</assemblyBinding>

but it didn' t work. I tried to put the C:\Chromium directory into the PATH env variable, but it didn' t work either. I always ended up with

System.IO.FileNotFoundException: 'Could not load file or assembly 'CefSharp.WinForms'

like exceptions. It looks like a very easy think to do but i got really stuck.

Any ideas would be really helpful. Thanks in advance

EDIT#1 Code Fragment

this.chromiumComponent = new CefSharp.WinForms.ChromiumWebBrowser();
this.SuspendLayout();
// 
// chromiumComponent
// 
this.chromiumComponent.ActivateBrowserOnCreation = false;
this.chromiumComponent.Dock = System.Windows.Forms.DockStyle.Fill;
this.chromiumComponent.Location = new System.Drawing.Point(0, 0);
this.chromiumComponent.Name = "chromiumComponent";
this.chromiumComponent.Size = new System.Drawing.Size(800, 450);
this.chromiumComponent.TabIndex = 0;

I reference the CefSharp, CefSharp.Core, CefSharp.WinForms dlls from C:\Chromium directory. My intention is not to load the dlls from a subfolder of the project. Instead of that, I would like to place the dlls to a generic directory(like C:\Chromium) and my app to use them from this directory.


Solution

  • Did you try to use AppDomain.AssemblyResolve event? If some assembly is missing in app output folder then AssemblyResolve event occurs and you can load assembly from different folder.

    [STAThread]
    static void Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        ...
    }
    
    private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        var requestedName = new AssemblyName(args.Name);
        var chromiumDirectory = @"C:\\C:\Chromium";
        var assemblyFilename = Path.Combine(chromiumDirectory, requestedName.Name + ".dll");
        if (File.Exists(assemblyFilename))
        {
            return Assembly.LoadFrom(assemblyFilename);
        }
        return null;
    }