Visual Studio 如何将文件从子文件夹发布到主应用程序文件夹?

ldioqlga  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(183)

我有一个使用ClickOnce发布的WinForms NET4.8桌面应用程序。我需要将项目子文件夹中的dll文件发布到与主应用程序相同的目录。我的csproj文件中包含以下内容:

<PropertyGroup>
    <UnreferencedDlls>lib\twaindsm.dll</UnreferencedDlls>
  </PropertyGroup>
  <PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>
            CustomCollectFiles;
            $(CopyAllFilesToSingleFolderForPackageDependsOn);
        </CopyAllFilesToSingleFolderForPackageDependsOn>
  </PropertyGroup>
  <Target Name="AfterBuild">
    <Message Text="Copying unreferenced DLLs to bin" Importance="High" />
    <CreateItem Include="$(UnreferencedDlls)">
      <Output TaskParameter="Include" ItemName="_UnReferencedDLLs" />
    </CreateItem>
    <Copy SourceFiles="@(_UnReferencedDLLs)" DestinationFolder="$(OutputPath)\%(RecursiveDir)" SkipUnchangedFiles="true" />
  </Target>
  <Target Name="CustomCollectFiles">
    <Message Text="Publishing unreferenced DLLs" Importance="High" />
    <ItemGroup>
      <_CustomFiles Include="$(UnreferencedDlls)" />
      <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
        <DestinationRelativePath>$(OutputPath)\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>

文件将发布到子文件夹。
我把这一行改成:

<DestinationRelativePath>$(OutputPath)\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>

致:

<DestinationRelativePath>$(OutputPath)\%(Filename)%(Extension)</DestinationRelativePath>

但还是不行。
如何强制将文件发布到与应用相同的文件夹?

j2datikz

j2datikz1#

我不知道它是否能帮到你:%(RecursiveDir)
如果Include属性包含通配符**,则此元数据指定替换通配符的路径部分。
也许你可以试着改变dll的路径。我把它放进ProjectDir\lib\Debug\,下面是我的代码:

<PropertyGroup>
  <UnreferencedDlls>lib\**\ClassLibrary1.dll</UnreferencedDlls>
</PropertyGroup>
<PropertyGroup>
  <CopyAllFilesToSingleFolderForPackageDependsOn>
        CustomCollectFiles;
        $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
<Target Name="AfterBuild">
  <Message Text="Copying unreferenced DLLs to bin" Importance="High" />
  <CreateItem Include="$(UnreferencedDlls)">
     <Output TaskParameter="Include" ItemName="_UnReferencedDLLs" />
  </CreateItem>
  <Copy SourceFiles="@(_UnReferencedDLLs)" DestinationFolder="bin\%(RecursiveDir)" SkipUnchangedFiles="true" />
</Target>
<Target Name="CustomCollectFiles">
  <Message Text="Publishing unreferenced DLLs" Importance="High" />
  <ItemGroup>
    <_CustomFiles Include="$(UnreferencedDlls)" />
    <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
      <DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
    </FilesForPackagingFromProject>
  </ItemGroup>
</Target>

你也可以参考this page,希望它能帮到你。

相关问题