Prevent Visual Studio from leaking pdb path in compiled exe

Viewed 448

When I build my C# project as a release in Visual Studio, the resulting .exe contains the following string:

C:\Users\jornane\source\repos\«solution»\«project»\obj\Release\«name».pdb

The .exe file leaks my username and the path where I store the project. Is there a way I can prevent this from happening? I understand why that path would be there for Debug builds, but not for Release.

1 Answers

It is possible to replace the stored PDB paths using the -pathmap compiler option, for example the following generates «name».exe with the PDB path stored as .\«name».pdb, instead of using the full path.

C:\Users\«user»\source\repos\«solution»\«project»>csc -debug -pathmap:"%cd%=." «name».cs

The -pathmap option is not exposed in the IDE, but can be set into the .csproj file directly, by adding the following near the end of the file.

<Project>
<!-- ... -->
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <!-- after 'Microsoft.CSharp.targets' for 'IntermediateOutputPath' to be defined -->
    <PathMap>$(MSBuildProjectDirectory)\$(IntermediateOutputPath)=.</PathMap>
  </PropertyGroup>
</Project>

Replace <PathMap> with <PathMap Condition="'$(Configuration)'=='Release'"> for the option to apply to release build(s), only.

Note: the replacement path is set to "." in the examples above because csc does not allow it to be empty (error CS8101: The pathmap option was incorrectly formatted). Any other non-empty user-defined string will work in place of . as well.

Related