如何在 Visual Studio 2017 为我创建的 NuGet 包中包含我的类库旁边的文本文件? [英] How do I include a text file alongside my class library in my NuGet package which Visual Studio 2017 is creating for me?

查看:17
本文介绍了如何在 Visual Studio 2017 为我创建的 NuGet 包中包含我的类库旁边的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本文件在我的类库项目中.如果更新,我将其构建操作设置为内容并将复制到输出目录设置为复制,因此 .csproj 有如下部分:

<内容包括="MyFile.txt"><CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory></内容></项目组>

我这样做是因为我需要使用我的类库中的文本文件,并且不能将它作为嵌入式资源,因为我想允许最终用户编辑它.当我使用 VS 2017 创建包时,它给了我一个 .nuspec,其中包含如下部分:

<contentFiles><文件包括="any/net46/MyFile.txt" buildAction="Content"/><文件包括="any/netstandard1.3/MyFile.txt" buildAction="Content"/></内容文件>

然后,当我从另一个项目引用包时,MyFile.txt 出现在项目的根目录中,其中 Build Action 设置为 Content,但 Copy to Output Directory 设置为 Do not copy.我只想将文件与 .dll 一起包含,而不是包含在引用项目中.

解决方案

以下将创建一个 nuget 包,当使用应用程序安装它时,它会将您选择的文件与包的 dll 一起持久保存,并且可以完全通过 VisualStudio 2017 IDE.

假设您的解决方案中有一个名为 Foo.Bar 的 C# 项目,您希望将其构建到 nuget 包中:

在 Visual Studio 中,右键单击项目 Foo.Bar >属性 并转到Package 选项卡.确保 Package id: 的值为 Foo.Bar.

在与 Foo.Bar.csproj 文件相同的目录中创建一个 Foo.Bar.targets 文件,其内容如下:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><项目组><无包含="$(MSBuildThisFileDirectory)MyFile.txt"><Link>MyFile.txt</Link><CopyToOutputDirectory>总是</CopyToOutputDirectory></无></项目组></项目>

在 Visual Studio 2017 中右键单击项目,然后选择 Edit Foo.Bar.csproj.将以下块放入打开的项目文件中:

<无包含="MyFile.txt"><包>真的</包><PackagePath>构建</PackagePath></无><None Include="Foo.Bar.targets"><包>真的</包><PackagePath>构建</PackagePath></无></项目组>

现在,当您右键单击项目并选择 Pack(或者如果您从项目所在目录的命令行中运行 dotnet pack),它将生成Foo.Bar 项目的 .nupkg 文件.MyFile.txtFoo.Bar.targets 将在它里面的 build 文件夹中.

作为测试,我将它安装到一个示例控制台应用项目中.完成此操作后,MyFile.txt 会出现在控制台应用程序的解决方案资源管理器中,并且可以打开和编辑.此外,如果我发布控制台应用程序,Foo.Bar.dllMyFile.txt 都在 bin >发布 >[目标框架] >publish 已发布文件的文件夹(如果使用文件夹配置文件发布到本地磁盘).

需要注意的一个有趣的事情是,在撰写本文时,如果您在安装包的控制台应用程序中右键单击 MyFile.txt 并选择 Properties,您将会看到 Copy to Output Directory 设置为 Always.如果我尝试将其更改为 Do Not Copy 或其他任何内容,当对话框关闭时,尽管单击 OK,它将恢复为 Always.就像您每次都单击 Cancel 一样.这是因为对生成 nuget 包的原始 Foo.Bar.csproj 文件进行了编辑,特别是:<CopyToOutputDirectory>Always</CopyToOutputDirectory>..p>

My text file is in my class library project. I have its Build Action set to Content and Copy to Output Directory set to Copy if newer, so the .csproj has a section like:

<ItemGroup>
  <Content Include="MyFile.txt">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

I'm doing this because I need to use the text file from my class library, and can't have it as an embedded resource because I want to allow end-users to edit it. When I use VS 2017 to create the package it gives me a .nuspec with a section like this:

<contentFiles>
  <files include="any/net46/MyFile.txt" buildAction="Content" />
  <files include="any/netstandard1.3/MyFile.txt" buildAction="Content" />
</contentFiles>

Then when I reference the package from another project, MyFile.txt appears at the root of the project with Build Action set to Content but Copy to Output Directory set to Do not copy. I just want the file to be included alongside the .dll, without being included in the referencing project.

解决方案

The following will create a nuget package that persists your chosen file(s) alongside your package's dll when a consuming application installs it and can be done entirely through the Visual Studio 2017 IDE.

Let's pretend you have a C# project called Foo.Bar in your solution that you would like to build into a nuget package:

In Visual Studio, right-click the project Foo.Bar > Properties and go to the Package tab. Make sure the value for Package id: is Foo.Bar.

Create a Foo.Bar.targets file in the same directory as your Foo.Bar.csproj file with the following content:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)MyFile.txt">
      <Link>MyFile.txt</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

Right click the project in Visual Studio 2017, and select Edit Foo.Bar.csproj. Put the following block in the opened project file:

<ItemGroup>
    <None Include="MyFile.txt">
      <Pack>true</Pack>
      <PackagePath>build</PackagePath>
    </None>
    <None Include="Foo.Bar.targets">
      <Pack>true</Pack>
      <PackagePath>build</PackagePath>
    </None>
  </ItemGroup>

Now when you right click the project and select Pack (or if you run dotnet pack from the command line in the directory where your project resides), it will generate a .nupkg file for your Foo.Bar project. MyFile.txt and Foo.Bar.targets will be in the build folder inside of it.

As a test I installed it into a sample Console App project. After doing so, MyFile.txt is present in the Solution Explorer of the Console App and can be opened and edited. Furthermore, if I publish the console app, both Foo.Bar.dll and MyFile.txt are in the bin > Release > [TargetFramework] > publish folder of the published files (if published using a Folder Profile to local disk).

An interesting thing to note is that at time of writing, if you right click MyFile.txt in the Console App that installed the package and select Properties, you'll see that Copy to Output Directory is set to Always. If I try to change it to Do Not Copy or anything else, it will revert back to Always when the dialog is closed despite clicking OK. It will act as if you click Cancel every time. This is because of the edit made to the original Foo.Bar.csproj file that produced the nuget package, specifically: <CopyToOutputDirectory>Always</CopyToOutputDirectory>.

这篇关于如何在 Visual Studio 2017 为我创建的 NuGet 包中包含我的类库旁边的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆