[英]How do I include a text file alongside my class library in my NuGet package which Visual Studio 2017 is creating for me?
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:我将其构建操作设置为内容并将复制到输出目录设置为复制(如果更新),因此 .csproj 具有如下部分:
<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:当我使用 VS 2017 创建包时,它给了我一个 .nuspec ,其中包含如下部分:
<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.然后,当我从另一个项目引用包时,MyFile.txt 出现在项目的根目录中,构建操作设置为内容,但复制到输出目录设置为不复制。 I just want the file to be included alongside the .dll, without being included in the referencing project.我只想将该文件包含在 .dll 旁边,而不是包含在引用项目中。
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.下面将创建一个 nuget 包,当消费应用程序安装它时,它将您选择的文件与包的 dll 一起保存,并且可以完全通过 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:假设您的解决方案中有一个名为Foo.Bar的 C# 项目,您希望将其构建到 nuget 包中:
In Visual Studio, right-click the project Foo.Bar > Properties
and go to the Package
tab.在 Visual Studio 中,右键单击项目Foo.Bar > Properties
并转到Package
选项卡。 Make sure the value for Package id:
is Foo.Bar
.确保Package id:
的值是Foo.Bar
。
Create a Foo.Bar.targets
file in the same directory as your Foo.Bar.csproj
file with the following content:创建Foo.Bar.targets
在相同的目录中的文件Foo.Bar.csproj
有以下内容的文件:
<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
.在 Visual Studio 2017 中右键单击该项目,然后选择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.现在,当您右键单击该项目并选择Pack
(或者如果您从项目所在目录中的命令行运行dotnet pack
)时,它将为您的Foo.Bar
项目生成一个.nupkg
文件。 MyFile.txt
and Foo.Bar.targets
will be in the build
folder inside of it. MyFile.txt
和Foo.Bar.targets
将位于其中的build
文件夹中。
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.执行此操作后, MyFile.txt
将出现在控制台应用程序的解决方案资源管理器中,并且可以打开和编辑。 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).此外,如果我发布控制台应用程序, Foo.Bar.dll
和MyFile.txt
都在bin > Release > [TargetFramework] > publish
文件的bin > Release > [TargetFramework] > publish
文件夹中(如果使用文件夹配置文件发布到本地磁盘)。
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
.需要注意的一件有趣的事情是,在撰写本文时,如果您在安装包的控制台应用程序中右键单击MyFile.txt
并选择Properties
,您将看到Copy to Output Directory
设置为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
.如果我尝试将其更改为Do Not Copy
或其他任何内容,尽管单击OK
,它仍会在对话框关闭时恢复为Always
。 It will act as if you click Cancel
every time.就像您每次单击Cancel
一样。 This is because of the edit made to the original Foo.Bar.csproj
file that produced the nuget package, specifically: <CopyToOutputDirectory>Always</CopyToOutputDirectory>
.这是因为对生成 nuget 包的原始Foo.Bar.csproj
文件进行了编辑,特别是: <CopyToOutputDirectory>Always</CopyToOutputDirectory>
。
If you want to keep .nuspec, then you can edit it manually and add copyToOutput="true"
attribute.如果你想保留 .nuspec,那么你可以手动编辑它并添加copyToOutput="true"
属性。
If you're using VS2017 and want to edit your .csproj, then you should be able to specify extra metadata in your .csproj file (and you can set custom build action if necessary):如果您使用的是 VS2017 并且想要编辑您的 .csproj,那么您应该能够在 .csproj 文件中指定额外的元数据(如果需要,您可以设置自定义构建操作):
<Content Include="MyFile.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<IncludeInPackage>true</IncludeInPackage>
<BuildAction>Content</BuildAction>
<CopyToOutput>true</CopyToOutput>
</Content>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.