CefSharp has a lot of dependencies and libraries that it requires to run. The build folders are cluttered. How can I move the required .dll and .pak dependencies to a sub folder?
First off, to make everything easier, I would recommend adding a folder to Visual Studios and putting all the required files in there. If you make this folder in explorer, click 'show all files' above your solution in the solution explorer:
This right click the folder(s) and file(s) you wish to include and choose 'include in project.'
Be sure to include all required CefSharp files - more info on github
You should end up with a file tree that looks similar to this:
Be sure to change 'Copy to Output Directy' to 'Copy always' under properties for all files.
Now for the code. Your solution should have an 'App.config' file (if not, google around and you'll find the way to generate one).
You are going to add a new assemblyBinding
and probing
element to it (MSDN - probing)
The probing
element tells windows it should look in additional folders for libraries. Thus we can load in all the required .dll's for CefSharp this way.
Example App.config:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="resources/cefsharp" />
</assemblyBinding>
</runtime>
</configuration>
Note: the path is relative the location of the .exe file.
Now that takes care of the .dll files, but we now need to change the settings for CefSharp so it knows where to look for the .pak files, as well as the locales, and BrowserSubprocess.exe.
To do this we are going to define all the file paths and manual assign them to CefSharp.
Here's an example of what it should look like:
// File location variables
static string lib, browser, locales, res;
[STAThread]
static void Main()
{
// Assigning file paths to varialbles
lib = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"resources\cefsharp\libcef.dll");
browser = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"resources\cefsharp\CefSharp.BrowserSubprocess.exe");
locales = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"resources\cefsharp\locales\");
res = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"resources\cefsharp\");
var libraryLoader = new CefLibraryHandle(lib);
bool isValid = !libraryLoader.IsInvalid;
Console.WriteLine($"Library is valid: {isValid}");
LoadForm();
libraryLoader.Dispose();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void LoadForm()
{
var settings = new CefSettings();
settings.BrowserSubprocessPath = browser;
settings.LocalesDirPath = locales;
settings.ResourcesDirPath = res;
Cef.Initialize(settings, shutdownOnProcessExit: false, performDependencyCheck: false);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CefWinForm());
}
All this is adapted from: https://github.com/cefsharp/CefSharp/issues/601
The original issue was difficult to follow completely and get working properly so I thought I'd share the knowledge in case anyone encounters similar trouble in the future.
Note: Visual studio will still include the .dll's, .pak's, .xml's, etc. in the output directory, but you can check if your build is successful by deleting the dependencies from your main folder (leaving the resources folder).
© 2022 - 2024 — McMap. All rights reserved.