Visual studio not copying content files from indirectly referenced project

前端 未结 5 674
滥情空心
滥情空心 2020-12-09 01:57

I have the following project structure:

Library1 <--[project reference]-- Library2 <--[ref]-- Executable
--------                          --------             


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 02:25

    EDIT:

    You can put all content in a separate project, set all its entries to "content" and "copy always" and reference that project from External and Executable

    -

    IMO you're looking for embedded resource, not for content files.

    When you compile Library 1 the content files are placed in its bin folder. When Library 2 is compiled the compiler recognizes referenced code and includes it (Library 1.dll) but the content files aren't recognized since they aren't mentioned anywhere in Library 2. The same goes when linking Library 2 to the executable.

    If your content files are relatively small (icons, templates etc) and you don't envision need to edit them if you were to lose the source code then you can embed them as resources and provide a public method to return the contents, such as:

     public static Stream GetResourceContent(string rName){
         string resName = System.Reflection.Assembly
             .GetExecutingAssembly().GetManifestResourceNames()
             .FirstOrDefault(rn => rn.EndsWith("."+rName));
        if(resName!=null)
            return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resName);
        else
            return null;
    }
    

    If your content is bound to change, such as templates etc, then consider including a copy with the executable project

提交回复
热议问题