简体   繁体   English

如何在 Visual Studio 2017 中向 NuGet 包添加其他文件?

[英]How do you add additional files to a NuGet package in Visual Studio 2017?

I recently moved to Visual Studio 2017 Community Edition.我最近搬到了 Visual Studio 2017 社区版。 It has 2 nice new features:它有两个不错的新功能:

  1. You don't need to explicitly include your source files in the csproj.您不需要在 csproj 中明确包含您的源文件。 It does this automatically.它会自动执行此操作。

  2. It can build NuGet packages directly.它可以直接构建 NuGet 包。

I want to package up my open source CodeFirstWebFramework DLL as a NuGet package.我想将我的开源CodeFirstWebFramework DLL 打包为 NuGet 包。 As well as including the DLL , the package has to include a whole directory tree of other files (including .js, .tmpl, .css and .md files).除了包含DLL ,该包还必须包含其他文件(包括 .js、.tmpl、.css 和 .md 文件)的整个目录树。

How do I tell Visual Studio that I want this directory tree included in the package?我如何告诉 Visual Studio 我希望这个目录树包含在包中?

From what information I have found with extensive searching, and ignoring all the out-of-date information that involves adding files to the csproj, all I could find was to place them in a contentFiles folder, but this does not seem to work.根据我通过广泛搜索找到的信息,并忽略所有涉及将文件添加到 csproj 的过时信息,我能找到的只是将它们放在contentFiles文件夹中,但这似乎不起作用。

My project file looks like this:我的项目文件如下所示:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net45</TargetFramework>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    <Authors>Nikki Locke</Authors>
    <Company>Trumphurst Ltd</Company>
    <Description>Easy to use web server for building web apps that use sql databases generated from c# classes</Description>
    <Copyright>2017 Trumphurst Ltd.</Copyright>
    <PackageProjectUrl>https://github.com/nikkilocke/CodeFirstWebFramework</PackageProjectUrl>
    <RepositoryUrl>https://github.com/nikkilocke/CodeFirstWebFramework</RepositoryUrl>
    <RepositoryType>Github</RepositoryType>
    <PackageTags>C# SQL Code First Web Server</PackageTags>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Markdig" Version="0.12.1" />
    <PackageReference Include="Mono.Data.Sqlite.Portable" Version="1.0.3.5" />
    <PackageReference Include="mustache-sharp" Version="0.2.10" />
    <PackageReference Include="MySql.Data" Version="6.9.9" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="System.Net" />
    <Reference Include="System.Web" />
  </ItemGroup>

</Project>

Wildly guessing what I might need to do from the conflicting and out-of-date information on the web, I have added the following:从网上的冲突和过时的信息中疯狂地猜测我可能需要做什么,我添加了以下内容:

  <ItemGroup>
    <Content Include="contentFiles/**/*.*" copyToOutput="true">
      <IncludeInPackage>true</IncludeInPackage>
    </Content>
  </ItemGroup>

Now the .nupkg file has the contents of the contentFiles folder inside it (in two places, a content folder, and a contentFiles folder).现在.nupkg文件中包含contentFiles文件夹的内容(在两个位置,一个content文件夹和一个contentFiles文件夹)。

However, when I install the package in another project, the content files do not appear, although they are listed in the project.assets.json file in the obj folder.但是,当我在另一个项目中安装该包时,虽然内容文件列在obj文件夹的project.assets.json文件中,但并未出现。

I have now changed my project file again, so it now reads:我现在再次更改了我的项目文件,因此它现在显示为:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net45</TargetFramework>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    <Authors>Nikki Locke</Authors>
    <Company>Trumphurst Ltd</Company>
    <Description>Easy to use web server for building web apps that use sql databases generated from c# classes</Description>
    <Copyright>2017 Trumphurst Ltd.</Copyright>
    <PackageProjectUrl>https://github.com/nikkilocke/CodeFirstWebFramework</PackageProjectUrl>
    <RepositoryUrl>https://github.com/nikkilocke/CodeFirstWebFramework</RepositoryUrl>
    <RepositoryType>Github</RepositoryType>
    <PackageTags>C# SQL Code First Web Server</PackageTags>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Markdig" Version="0.12.1" />
    <PackageReference Include="Mono.Data.Sqlite.Portable" Version="1.0.3.5" />
    <PackageReference Include="mustache-sharp" Version="0.2.10" />
    <PackageReference Include="MySql.Data" Version="6.9.9" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="System.Net" />
    <Reference Include="System.Web" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="contentFiles/**/*.*" copyToOutput="true">
      <IncludeInPackage>true</IncludeInPackage>
      <CopyToOutput>true</CopyToOutput>
      <BuildAction>Content</BuildAction>
      <copyToOutput>true</copyToOutput>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <Compile Remove="Phone\**" />
    <EmbeddedResource Remove="Phone\**" />
    <None Remove="Phone\**" />
  </ItemGroup>

</Project>

The contentFiles are now successfully copied into a folder of the same name (ie contentFiles ) in any project that downloads the NuGet package - not sure which directive worked, but one of them did.现在, contentFiles 已成功复制到下载 NuGet 包的任何项目中的同名文件夹(即contentFiles )中 - 不确定哪个指令有效,但其中一个有效。

The content is not automatically copied to the project's output folder on compile, unfortunately - suggestions welcome.不幸的是,内容不会在编译时自动复制到项目的输出文件夹中 - 欢迎提出建议。

First, please be aware that you need to put your files either into a content or contentFiles folder depending on how you NuGet package is referenced.首先,请注意,您需要将文件放入 content 或 contentFiles 文件夹中,具体取决于引用 NuGet 包的方式。 See the end of this section .请参阅本节末尾。

Basically, if a NuGet project is referenced by package.config file, the files from the content folder of the NuGet package will be copied to the referencing project.基本上,如果一个 NuGet 项目被 package.config 文件引用,NuGet 包内容文件夹中的文件将被复制到引用项目。 If the NuGet package is referenced by PackageReference in the project file, the files from the contentFiles folder within the package will be used.如果项目文件中的 PackageReference 引用了 NuGet 包,则将使用包内 contentFiles 文件夹中的文件。 It is recommended to include both.建议两者都包含。

The structure of the contentFiles folder can be found here . contentFiles 文件夹的结构可以在这里找到。 It is possible to specify the language and the framework for the files to be copied.可以为要复制的文件指定语言和框架。

So in your case:所以在你的情况下:

<ItemGroup>
    <Content Include="contentFiles/**/*.*">
        <Pack>true</Pack>
        <PackagePath>contentFiles\any\any;content</PackagePath>
    </Content>
</ItemGroup>

I got the solution.我得到了解决方案。


Below first snippet of code is not sufficient in order to copy files on target application下面的第一段代码不足以复制目标应用程序上的文件

<ItemGroup>
<Content Include="contentFiles/**/*.*">
    <Pack>true</Pack>
    <PackagePath>contentFiles\any\any;content</PackagePath>
</Content>

Solution -: This will copy files on root folder.解决方案-:这将复制根文件夹中的文件。

<ItemGroup>
<Content Include="contentFiles/**/*.*">
    <Pack>true</Pack>
    <PackagePath>contentFiles;content</PackagePath>
    <IncludeInPackage>true</IncludeInPackage>
    <CopyToOutput>true</CopyToOutput>
    <BuildAction>Content</BuildAction>
    <copyToOutput>true</copyToOutput>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    <CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>

This is working for me.这对我有用。 Hope it will work for you.希望它对你有用。

包装在“Content”和“IncludeInPackage”中足以将文件复制到包中。

Addition to above除了以上

Below image has my project contentFiles folder structure下图有我的项目 contentFiles 文件夹结构

附加的代码图像具有我的 contentFiles 文件夹结构

Design of my .csproj file我的 .csproj 文件的设计

我的 .Csproj 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio Professional 2017中的Nuget程序包 - Nuget Package in Visual Studio Professional 2017 如何将其他文件添加到 nuget package 并在 nuget ZEFE90A8E604A7C67D803A 代码中引用这些文件? - How to add additional files to a nuget package and reference those files within the nuget package code? 增加Nuget包版本NetStandard本地文件夹视觉工作室2017 - Increment Nuget Package Version NetStandard Local Folder visual studio 2017 如何在NuGet包中使用其他文件? - How to use additional files in NuGet package? 如何在 Visual Studio 中启用 NuGet Package 还原? - How do I enable NuGet Package Restore in Visual Studio? Visual Studio 2017中“添加程序包”灯泡在哪里? - Where is the “Add package” lightbulb in Visual Studio 2017? 如何向Visual Studio 2017添加功能? - How do I add features to Visual Studio 2017? 如何在未安装 Nuget 或 Visual Studio 的 Web 服务器上安装、更新、注册 Nuget package? - How do I Install, Update, Register a Nuget package on the Web Server that does not have Nuget or Visual Studio installed? 如何在visual studio中调试nuget包 - How to debug nuget package in visual studio 如何恢复 package Visual Studio 2017 - How to restore package Visual Studio 2017
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM