How do I prevent 'Publish Web' from overwriting config files?

In Visual Studio solution explorer, go to your web.config file's properties. Make sure "Build Action" is "None" and "Copy to Output Directory" is "Do not copy".

If you ever want to update it in the future you'll have to do it manually or change "Build Action" back to "Content". The next time you build (or publish) it will overwrite it.


I solved it by adding this exclusion:

<ExcludeFilesFromDeployment>Web.config</ExcludeFilesFromDeployment>

to the project file (.csproj or .vbproj), inside these nodes:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

and

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

See Web Deployment: Excluding Files and Folders via the Web Application’s Project File.