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

MSBuild ZipDirectory任务路径分隔符不一致问题求助

Fix MSBuild ZipDirectory Task Producing Backslashes in ZIP Paths (VS2017 Professional)

Let’s tackle this frustrating issue where your VS2017 Professional setup generates ZIP files with backslashes (breaking cross-platform deployment), while VS2017 Community uses forward slashes as expected. Even though .NET 4.6.1+ supports forward slashes in ZIP paths, the MSBuild ZipDirectory task might not be picking up the correct runtime configuration in your Pro environment. Here are actionable solutions:

Solution 1: Modify MSBuild.exe.config to Force Forward Slashes

The Switch.System.IO.Compression.ZipFile.UseBackslash app context switch does work for MSBuild tasks—you just need to apply it to the MSBuild process itself, not your application. Here’s how:

  1. Locate the MSBuild.exe.config for your VS2017 Professional installation. It’s typically at:
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config
    
  2. Open the file in an editor with admin rights, and add the following inside the <runtime> section:
    <AppContextSwitchOverrides value="Switch.System.IO.Compression.ZipFile.UseBackslash=false" />
    
    If there’s already an AppContextSwitchOverrides entry, append the switch to its value (separated by semicolons):
    <AppContextSwitchOverrides value="existingSwitch=value;Switch.System.IO.Compression.ZipFile.UseBackslash=false" />
    
  3. Save the file and re-run your MSBuild batch script. This forces the MSBuild process (and its tasks, including ZipDirectory) to use forward slashes in ZIP paths.

Solution 2: Replace ZipDirectory with a Custom PowerShell Script

If modifying the MSBuild config isn’t feasible, use PowerShell’s Compress-Archive cmdlet (which uses forward slashes when targeting .NET 4.6.1+) instead of the built-in ZipDirectory task. Update your MSBuild target:

<Target Name="ZipRelease" DependsOnTargets="getversion;gettime">
  <MakeDir Directories="$(ReleaseDir)" Condition="!Exists('$(ReleaseDir)')" />
  <Exec Condition="Exists('$(BuildDir)')" 
        Command="powershell -Command &quot;Compress-Archive -Path '$(BuildDir)\*' -DestinationPath '$(ReleaseDir)\$(MODNAME)-$(DLLVersion)_$(CurrentDate).zip' -Force&quot;" />
</Target>

This bypasses the MSBuild ZipDirectory task entirely and uses PowerShell’s implementation, which honors the .NET runtime’s path separator behavior.

Solution 3: Use 7-Zip Command Line for Full Control

For even more control over ZIP path formatting, use the 7-Zip command-line tool. First, ensure 7-Zip is installed and its 7z.exe is in your system PATH (or specify the full path to it):

<Target Name="ZipRelease" DependsOnTargets="getversion;gettime">
  <MakeDir Directories="$(ReleaseDir)" Condition="!Exists('$(ReleaseDir)')" />
  <Exec Condition="Exists('$(BuildDir)')" 
        Command="7z a -tzip '$(ReleaseDir)\$(MODNAME)-$(DLLVersion)_$(CurrentDate).zip' '$(BuildDir)\*' -mx=5" />
</Target>

7-Zip uses forward slashes in ZIP paths by default, so this will produce cross-platform-compatible archives regardless of your VS/MSBuild environment.

Why Your Previous Attempts Didn’t Work

  • Specifying ToolsVersion="15.8" sets the MSBuild toolset version, but doesn’t force the task to use a specific .NET runtime version. The ZipDirectory task in VS2017 Pro might be using an older runtime binding that doesn’t respect the forward slash setting unless explicitly configured via the app context switch.
  • Uninstalling old SDKs doesn’t affect the runtime MSBuild uses, since MSBuild has its own bundled runtime components in the VS installation directory.

内容的提问来源于stack exchange,提问作者Micha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:25:42