[英]Find resources of Class Library from Main Project
The Problem问题
My solution is made up of a handful of projects.我的解决方案由少数几个项目组成。 The main one for the UI we'll call Assistant, and the backend we'll call AssistantLib. UI 的主要部分我们称为 Assistant,后端我们称为 AssistantLib。
Here is the structure:这是结构:
In AssistantLib I have included PDFs as resources in a Resources folder with a Build Action
of Content and Copy to Output Directory
as Copy Always .在 AssistantLib 中,我将 PDF 作为资源包含在 Resources 文件夹中,其中包含内容的Build Action
,并将Copy to Output Directory
作为始终复制。 I am able to locate them when debugging by using a combo of these:通过使用以下组合,我可以在调试时找到它们:
private string GetArtifactPath(string artifactName)
{
return Path.Combine(GetResourcePath(), artifactName);
}
public static string GetResourcePath()
{
return Path.Combine(Directory.GetCurrentDirectory(), "Resources");
}
This works.这有效。 Once I return the string from GetArtifactPath, I open the File with a Process
object and AcroRd32.exe
.从 GetArtifactPath 返回字符串后,我打开带有Process
对象和AcroRd32.exe
的文件。
Note that I need to reference these files by their file paths.请注意,我需要通过它们的文件路径来引用这些文件。 They're not simple txts to read or stream.它们不是易于阅读或流式传输的简单 txt。 I also need to be able to open them with certain flag provided by AcroRd32.exe
.我还需要能够使用AcroRd32.exe
提供的某些标志打开它们。 That means I must have the file path .这意味着我必须有文件路径。
The problem I'm having as that once I publish the ClickOnce app, I receive an error that the file couldn't be found:我遇到的问题是,一旦我发布 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)
And of course, when I go to that location, the resources aren't there.当然,当我去那个位置时,资源不在那里。
What I've Tried我试过的
Build Action
to Embedded Resource & None - same result我已将Build Action
更改为 Embedded Resource & None - 结果相同Copy Output Directory
to Do Not Copy我已将Copy Output Directory
更改为不复制MyNamespace.Properties.Resources.My_Zip_File;
这也不适用,因为引用MyNamespace.Properties.Resources.My_Zip_File;
doesn't work in my situation.在我的情况下不起作用。 I don't know the name of the resource ahead of time.我提前不知道资源的名称。Update更新
I am looking into post-build events.我正在调查构建后事件。 In doing so, I found that these resources are already in the output directory following a build:在这样做时,我发现这些资源已经在构建后的输出目录中:
But for some reason they don't show up when I publish:但由于某种原因,当我发布时它们没有出现:
Update 2更新 2
To illustrate how the folder structure affects this, here are before and afters.为了说明文件夹结构如何影响这一点,这里是之前和之后。
When a Resource is in AssistantLib
(eg EVMSBP), this is the structure:当资源在AssistantLib
(例如 EVMSBP)中时,结构如下:
And here is what the ClickOnce publish folder looks like after installing with the resource in AssistantLib
:这是使用AssistantLib
的资源安装后 ClickOnce 发布文件夹的样子:
Alternatively, when a Resource is in Assistant
(again EVMSBP), this is the structure:或者,当一个资源在Assistant
(又是 EVMSBP)中时,这是结构:
And here's what the ClickOnce publish folder looks like after installing with the resource in Assistant
:以下是使用Assistant
的资源安装后 ClickOnce 发布文件夹的样子:
From what I can tell, the Resource MUST be a part of the Startup project.据我所知,资源必须是启动项目的一部分。 That sounds like insanity?这听起来像疯子?
What am I missing?我错过了什么?
With more details from the edit, this is how I replicated it随着编辑的更多细节,这就是我复制它的方式
In AssistLib project在AssistLib项目中
Build Action
as Embedded Resources
.具有Build Action
资源中的文件作为Embedded Resources
。 Output Type:
Class Library
(AssistLib.dll) Output Type:
Class Library
(AssistLib.dll)
See Solution Structure请参阅解决方案结构
AssistLib.dll
我引用了AssistLib.dll
AzTech.cs 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);
}
}
}
i was facing same problem when i was building the project on exe format using the visual studio setup i simple add pdf file in project build output directory .当我使用 Visual Studio 设置以 exe 格式构建项目时,我遇到了同样的问题,我只是在项目构建输出目录中添加了 pdf 文件。 you have to copy your pdf folder in bin or release folder and use ~ root directory to get file您必须将 pdf 文件夹复制到bin或release文件夹中,然后使用 ~ 根目录来获取文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.