Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.NETCore中包含库中的文件_C#_.net_.net Core - Fatal编程技术网

C# 如何在.NETCore中包含库中的文件

C# 如何在.NETCore中包含库中的文件,c#,.net,.net-core,C#,.net,.net Core,我有一个项目A,它依赖于项目B 在项目B中,我在构建中包括以下附加文件: <ItemGroup> <None Include="jsfiles/**/*" CopyToOutputDirectory="PreserveNewest" /> </ItemGroup> File.ReadAllText("jsfiles/foo.js"); 这将成功地将文件复制到out目录中。但是当我在项目A中包含项目B时,运行时是相对于项目A的根而不是输出目录解析文

我有一个项目A,它依赖于项目B

在项目B中,我在构建中包括以下附加文件:

<ItemGroup>
    <None Include="jsfiles/**/*"  CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
File.ReadAllText("jsfiles/foo.js");
这将成功地将文件复制到out目录中。但是当我在项目A中包含项目B时,运行时是相对于项目A的根而不是输出目录解析文件。因为这些文件不存在于项目A的根目录中(但它们确实存在于项目A的输出目录中),所以找不到它们

在项目B中加载文件的正确方法是什么,这样当项目B包含在项目A中时,它仍然可以工作


基本上,我只是在寻找在项目中包含额外资源的正确方法。

您可以使用链接。您需要将项目A中的链接添加到项目B中的文件:

<ItemGroup>  
  <Content Include="..\Project B\jsfiles\foo.js" Link="jsfiles\foo.js" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>  


尽管您必须使用此方法创建指向文件夹中每个文件的链接。

我们的解决方案层次结构:

ProjectA
  Startup.cs       // Uses TextService.cs that uses Text.txt
  ProjectA.csproj  // Does not want to now anything about how TextService works
                   // just has a dependency on Project B
ProjectB
  Text.txt
  TextService.cs   // This one uses Text.txt file
  ProjectB.csproj  // Contains EmbeddedResource with Text.txt here
您的用例可能不同,但我发现对我们有用的是使用
EmbeddedResource
而不是
csproj
文件中的
Content
标记。通过这种方式,该文件将包含在上述两种情况中:

  • 当构建
    ProjectB
  • 发布
    ProjectA
    时(具有
    ProjectB
    依赖关系)
项目b.csproj
中的代码块:

<ItemGroup>
  <EmbeddedResource Include="path\to\Text.txt">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </EmbeddedResource>
</ItemGroup>

保存最新

或者,可以在Visual Studio(2019年测试)中通过转到
txt
文件的属性并设置
Build Action:Embedded resource

找到解决方案了吗?我们面临着类似的问题:/