【发布时间】:2021-05-12 02:38:27
【问题描述】:
我想在 nuget 包中提供一些额外的文件,以便无论何时何地安装它,这些文件都可以在正确的位置提供。
我遇到的问题是我的代码需要引用这些文件才能运行。
文件是单个目录中的 3 个.exe 文件和一个.jar 文件...
这是引用该文件夹位置的代码...
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\WebDrivers\\Drivers\\";
我希望能够打包此代码和文件,以便在安装此 nuget 包时,此代码将指向有效位置。
我该怎么做?
我尝试的第一件事是将以下内容添加到.csproj 文件中
<ItemGroup>
<None Update="WebDrivers\Drivers\chromedriver.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="WebDrivers\Drivers\geckodriver.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="WebDrivers\Drivers\msedgedriver.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="WebDrivers\Drivers\selenium-server-standalone-3.141.0.jar">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
仅当您在同一解决方案中引用该项目时,这才有效。在您完成dotnet pack 并将软件包安装到不同的项目/解决方案中后,它就不起作用了。
我尝试的第二件事是这里接受的答案......
Copy files from Nuget package to output directory with MsBuild in .csproj and dotnet pack command
但我无法复制任何文件...
SASelenium.Framework.csproj
<ItemGroup Label="FilesToCopy">
<Content Include="SASelenium.Framework.targets" PackagePath="build/SASelenium.Framework.targets" />
<Content Include="LogFiles\*.config" Pack="true" PackagePath="contentFiles\LogFiles">
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
</ItemGroup>
SASelenium.Framework.targets
<ItemGroup>
<LogFiles Include="$(MSBuildThisFileDirectory)\..\contentFiles\LogFiles\*.config" />
</ItemGroup>
<Target Name="CopyLogFiles" BeforeTargets="Build">
<Copy SourceFiles="@(LogFiles)" DestinationFolder="$(TargetDir)CopiedLogFiles\" />
</Target>
当我构建安装了这个包的项目时,我没有看到任何文件。
即使这确实有效,我如何确保包中的代码在从任何项目运行时始终指向这些文件?
【问题讨论】:
-
但我无法复制任何文件...来自不同问题的目标用于
*.config文件。您是否尝试在复制粘贴之前查看文件扩展名?你的*.config文件在哪里? -
@PavelAnikhouski 它们显示在屏幕截图中。我在
LogFiles目录中有一个test.config
标签: c# asp.net-core nuget dotnet-cli