如何将 CefSharp 依赖项和文件移动到子目录?
How to move CefSharp dependencies and files to subdirectory?
CefSharp 有许多 运行 需要的依赖项和库。构建文件夹杂乱无章。如何将所需的 .dll 和 .pak 依赖项移动到子文件夹?
首先,为了让一切变得更简单,我建议在 Visual Studios 中添加一个文件夹,并将所有需要的文件放入其中。如果您在资源管理器中创建此文件夹,请在解决方案资源管理器中单击解决方案上方的 'show all files':
右键单击您要包含的文件夹和文件,然后选择 'include in project.'
确保包含所有必需的 CefSharp 文件 - more info on github
您最终应该得到一个类似于此的文件树:
确保在所有文件的属性下将 'Copy to Output Directy' 更改为 'Copy always'。
现在是代码。您的解决方案应该有一个 'App.config' 文件(如果没有,google 左右,您会找到生成一个的方法)。
您将向其中添加新的 assemblyBinding
和 probing
元素 (MSDN - probing)
probing
元素告诉 windows 它应该在其他文件夹中查找库。因此我们可以通过这种方式加载 CefSharp 所需的所有 .dll。
示例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>
注意:路径是相对于 .exe 文件的位置。
现在已经处理了 .dll 文件,但我们现在需要更改 CefSharp 的设置,以便它知道在哪里寻找 .pak 文件,以及语言环境和 BrowserSubprocess.exe。
为此,我们将定义所有文件路径并手动将它们分配给 CefSharp。
下面是它的外观示例:
// 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());
}
本文全部改编自:https://github.com/cefsharp/CefSharp/issues/601
最初的问题很难完全遵循并正常工作,所以我想我会分享这些知识,以防将来有人遇到类似的问题。
注意:Visual studio 仍会在输出目录中包含 .dll、.pak、.xml 等,但您可以通过删除来自主文件夹的依赖项(离开资源文件夹)。
CefSharp 有许多 运行 需要的依赖项和库。构建文件夹杂乱无章。如何将所需的 .dll 和 .pak 依赖项移动到子文件夹?
首先,为了让一切变得更简单,我建议在 Visual Studios 中添加一个文件夹,并将所有需要的文件放入其中。如果您在资源管理器中创建此文件夹,请在解决方案资源管理器中单击解决方案上方的 'show all files':
右键单击您要包含的文件夹和文件,然后选择 'include in project.'
确保包含所有必需的 CefSharp 文件 - more info on github
您最终应该得到一个类似于此的文件树:
确保在所有文件的属性下将 'Copy to Output Directy' 更改为 'Copy always'。
现在是代码。您的解决方案应该有一个 'App.config' 文件(如果没有,google 左右,您会找到生成一个的方法)。
您将向其中添加新的 assemblyBinding
和 probing
元素 (MSDN - probing)
probing
元素告诉 windows 它应该在其他文件夹中查找库。因此我们可以通过这种方式加载 CefSharp 所需的所有 .dll。
示例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>
注意:路径是相对于 .exe 文件的位置。
现在已经处理了 .dll 文件,但我们现在需要更改 CefSharp 的设置,以便它知道在哪里寻找 .pak 文件,以及语言环境和 BrowserSubprocess.exe。
为此,我们将定义所有文件路径并手动将它们分配给 CefSharp。
下面是它的外观示例:
// 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());
}
本文全部改编自:https://github.com/cefsharp/CefSharp/issues/601
最初的问题很难完全遵循并正常工作,所以我想我会分享这些知识,以防将来有人遇到类似的问题。
注意:Visual studio 仍会在输出目录中包含 .dll、.pak、.xml 等,但您可以通过删除来自主文件夹的依赖项(离开资源文件夹)。