MSBuild ZipDirectory任务路径分隔符不一致问题求助
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:
- 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 - Open the file in an editor with admin rights, and add the following inside the
<runtime>section:
If there’s already an<AppContextSwitchOverrides value="Switch.System.IO.Compression.ZipFile.UseBackslash=false" />AppContextSwitchOverridesentry, append the switch to its value (separated by semicolons):<AppContextSwitchOverrides value="existingSwitch=value;Switch.System.IO.Compression.ZipFile.UseBackslash=false" /> - 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 "Compress-Archive -Path '$(BuildDir)\*' -DestinationPath '$(ReleaseDir)\$(MODNAME)-$(DLLVersion)_$(CurrentDate).zip' -Force"" /> </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. TheZipDirectorytask 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

