首页 > 解决方案 > 从主项目中查找类库的资源

问题描述

问题

我的解决方案由几个项目组成。UI 的主要部分我们称之为 Assistant,后端我们称之为 AssistantLib。

这是结构:

在此处输入图像描述

在 AssistantLib 中,我将 PDF 作为资源包含在 Resources 文件夹中,其中包含 a of Build ActionContentCopy 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)

当然,当我去那个地方时,资源并不存在。

我试过的

更新

我正在研究构建后的事件。在这样做的过程中,我发现这些资源已经在构建之后的输出目录中:

在此处输入图像描述

但是由于某种原因,当我发布时它们没有出现:

在此处输入图像描述

更新 2

为了说明文件夹结构如何影响这一点,这里是之前和之后。

当有资源时AssistantLib(例如 EVMSBP),结构如下:

库中的资源

这是使用以下资源安装后 ClickOnce 发布文件夹的样子AssistantLib

失踪

或者,当资源位于Assistant(再次是 EVMSBP)中时,结构如下:

助理中的资源

这是使用资源安装后 ClickOnce 发布文件夹的样子Assistant

不缺

据我所知,资源必须是启动项目的一部分。这听起来很疯狂?

我错过了什么?

标签: c#.netclickoncevisual-studio-2019

解决方案


从编辑中获得更多细节,这就是我复制它的方式

  • AssistLib项目中

    • 资源中的文件Build ActionEmbedded 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);
            }
        }
 }


推荐阅读