c# - 从主项目中查找类库的资源
问题描述
问题
我的解决方案由几个项目组成。UI 的主要部分我们称之为 Assistant,后端我们称之为 AssistantLib。
这是结构:
在 AssistantLib 中,我将 PDF 作为资源包含在 Resources 文件夹中,其中包含 a of Build Action
Content和Copy Always。通过使用以下组合进行调试时,我可以找到它们:Copy to Output Directory
private string GetArtifactPath(string artifactName)
{
return Path.Combine(GetResourcePath(), artifactName);
}
public static string GetResourcePath()
{
return Path.Combine(Directory.GetCurrentDirectory(), "Resources");
}
这行得通。从 GetArtifactPath 返回字符串后,我会使用一个Process
对象打开文件,然后AcroRd32.exe
.
请注意,我需要通过文件路径引用这些文件。它们不是阅读或流式传输的简单 txt。我还需要能够使用提供的某些标志打开它们AcroRd32.exe
。这意味着我必须有文件路径。
发布 ClickOnce 应用程序后,我遇到的问题是找不到文件的错误:
Error: Could not find a part of the path 'C:\Users\EL-C\AppData\Local\Apps\2.0\3JCPDD49.7G5\9122AMZE.NZL\azte..tion_edea8654ffceff97_0001.0000_447ed0da08290357\Resources\Guidelines\3.2'.. Stacktrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
当然,当我去那个地方时,资源并不存在。
我试过的
- 我已将其更改
Build Action
为 Embedded Resource & None - 结果相同 - 我已将其更改
Copy Output Directory
为请勿复制 - 这不适用,因为我无法流式传输这些资源,它们不是简单的文本文件。我也不是在尝试阅读它们,而是在尝试打开它们。
- 这也不适用,因为
MyNamespace.Properties.Resources.My_Zip_File;
在我的情况下引用不起作用。我不提前知道资源的名称。
更新
我正在研究构建后的事件。在这样做的过程中,我发现这些资源已经在构建之后的输出目录中:
但是由于某种原因,当我发布时它们没有出现:
更新 2
为了说明文件夹结构如何影响这一点,这里是之前和之后。
当有资源时AssistantLib
(例如 EVMSBP),结构如下:
这是使用以下资源安装后 ClickOnce 发布文件夹的样子AssistantLib
:
或者,当资源位于Assistant
(再次是 EVMSBP)中时,结构如下:
这是使用资源安装后 ClickOnce 发布文件夹的样子Assistant
:
据我所知,资源必须是启动项目的一部分。这听起来很疯狂?
我错过了什么?
解决方案
从编辑中获得更多细节,这就是我复制它的方式
在AssistLib项目中
- 资源中的文件
Build Action
为Embedded Resources
. Output Type:
Class Library
(AssistLib.dll)查看解决方案结构
- 资源中的文件
- 在AzTech主项目
中
- 我参考了
AssistLib.dll
- 检查我要加载的资源(pdf)是否存在于 AssistLib.dll 中
- 加载资源
- 启动 PDF
- 到现在为止还挺好
- 我发布了应用程序并安装了
- 我从开始菜单上的图标启动了应用程序
- 哪个启动作为资源嵌入到 DLL 中的 PDF
- 我参考了
AzTech.cs
var nameOfTheFile = "test.pdf";
ResourceManager.GetResourceInfo(nameOfTheFile);
if (ResourceManager.resourceExists == false)
{ Console.WriteLine("Specified PDF file not found"); return; }
Console.WriteLine("Resouce found in DLL");
ResourceManager.LoadResource(nameOfTheFile);//Will load the pdf in your main project
Process.Start(nameOfTheFile);
class ResourceManager
{
public static bool resourceExists { get; set; } = false;
private static Stream resourceStream { get; set; }
public static void GetResourceInfo(string fileNameWithExtension)
{
const string pathToResource = "AssistantLib.Resources.Guidelines";
var assembly = Assembly.Load("AssistantLib");
//var names = assembly.GetManifestResourceNames();
var stream = assembly.GetManifestResourceStream($"{pathToResource}.{fileNameWithExtension}");
if (stream == null)
return;
resourceExists = true;
resourceStream = stream;
}
public static void LoadResource(string newFileNameWithExtension)
{
if(File.Exists(newFileNameWithExtension))
{
Console.WriteLine("File already exists");
return;
}
using (Stream s = File.Create(newFileNameWithExtension))
{
Console.WriteLine("Loading file");
resourceStream.CopyTo(s);
}
}
}
推荐阅读
- javascript - 如何阻止提交按钮刷新?
- python - 如何使用对数刻度 x 轴制作热图
- python - 具有 NaN 值的二维数组上的快速滑动窗口均值和标准偏差
- ruby - 缺少要链接的主机!请提供 :host 参数,设置 default_url_options[:host]
- bash - 如何在服务器中运行期望脚本以进行 SSH 并在 bash 脚本中设置值
- java - 如何用画布预览当前绘制的对象?
- javascript - 使用 firebase javascript 登录 Facebook 时出错
- git - 如何在本地 Git 工作目录中找到文件被删除的时间?
- php - 从 mongodb 获取数据并在 html 表中显示
- php - Laravel 表单未提交