You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将子文件夹文件发布至WinForms主应用程序目录?

解决ClickOnce发布时将twaindsm.dll部署到应用根目录的问题

针对你的场景,问题出在两处配置逻辑上:

  • AfterBuild目标的复制任务保留了原文件的子目录结构,导致编译时DLL就被放到了bin目录的子文件夹中
  • CustomCollectFiles里的DestinationRelativePath错误引入了输出路径变量,不符合ClickOnce发布路径的规则

修改后的完整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>
  <!-- 直接复制到输出目录根,移除%(RecursiveDir)以消除子目录结构 -->
  <Copy SourceFiles="@(_UnReferencedDLLs)" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true" />
</Target>
<Target Name="CustomCollectFiles">
  <Message Text="Publishing unreferenced DLLs" Importance="High" />
  <ItemGroup>
    <_CustomFiles Include="$(UnreferencedDlls)" />
    <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
      <!-- 直接指定文件名,移除$(OutputPath)和%(RecursiveDir),确保发布到应用根目录 -->
      <DestinationRelativePath>%(Filename)%(Extension)</DestinationRelativePath>
    </FilesForPackagingFromProject>
  </ItemGroup>
</Target>

更简洁的替代方案

如果不想维护自定义Target,可以直接在csproj中添加Content元素标记该DLL,一步完成编译复制和发布部署:

<Content Include="lib\twaindsm.dll">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  <Link>twaindsm.dll</Link>
  <PublishState>Include</PublishState>
  <Visible>false</Visible>
</Content>

这个配置会在编译时将DLL复制到输出根目录,同时ClickOnce发布时自动将其部署到应用根目录,无需额外自定义任务。

内容的提问来源于stack exchange,提问作者Ivan-Mark Debono

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 02:50:22