如何在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?

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

问题描述

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

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

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

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

然后,当我从另一个项目引用该程序包时,MyFile.txt出现在项目的根目录下,构建操作"设置为内容",但复制到输出目录"设置为不复制".我只希望文件包含在.dll中,而不包含在引用项目中.

解决方案

以下内容将创建一个nuget程序包,当使用消耗性的应用程序安装该程序时,该程序将您选择的文件与程序包的dll一起保留,并且可以完全通过Visual Studio完成.Studio 2017 IDE.

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

在Visual Studio中,右键单击项目 Foo.Bar>.属性,然后转到包装标签.确保 Package id:的值是 Foo.Bar .

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

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

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

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

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

作为测试,我将其安装到示例Console App项目中.之后, MyFile.txt 将出现在控制台应用程序的解决方案资源管理器中,并且可以打开和编辑.此外,如果我发布控制台应用程序,则 Foo.Bar.dll MyFile.txt 都位于 bin>中.发布>[TargetFramework]>已发布文件的发布" 文件夹(如果使用文件夹配置文件"发布到本地磁盘).

值得注意的是,在编写本文时,如果在安装该软件包的控制台应用程序中右键单击 MyFile.txt ,然后选择 Properties ,将会看到复制到输出目录设置为始终.如果我尝试将其更改为 Do Not Copy 或其他任何内容,则即使单击 OK ,关闭对话框后,它将恢复为 Always .就像您每次单击 Cancel (取消)一样.这是因为对生成nuget包的原始 Foo.Bar.csproj 文件所做的编辑,具体是:< CopyToOutputDirectory> Always</CopyToOutputDirectory> .

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天全站免登陆