copy-to-output-directory

Selects the correct MSBuild CopyToOutputDirectory mode for build output file copying.

Updated Jul 12, 2026
One-click install
npx skills add https://github.com/Patrick-Rex/DotNetTechSamples --skill copy-to-output-directory-patrick-rex
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: copy-to-output-directory
Source: https://github.com/Patrick-Rex/DotNetTechSamples/tree/main/.agents/plugins/dotnet-msbuild/skills/copy-to-output-directory
Command: npx skills add https://github.com/Patrick-Rex/DotNetTechSamples --skill copy-to-output-directory-patrick-rex

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Choosing the wrong CopyToOutputDirectory mode causes either stale files in bin/ or an unnecessary copy on every build, slowing down incremental builds. This Skill explains the four MSBuild copy modes and when each applies. ## Core Features & Use Cases - Mode Selection Guidance: Compares Never, PreserveNewest, Always, and IfDifferent (MSBuild 17.13+) with their copy triggers and incremental costs. - Performance Remediation: Explains how to eliminate the per-build copy cost of Always using IfDifferent or the $(SkipUnchangedFilesOnCopyAlways) property. - Mutated Output Reset: Covers the scenario where a test run or app mutates a copied file (SQLite database, config, state file) and each build must restore the source version. - Use Case: A test suite mutates a copied fixture database between builds. Replace CopyToOutputDirectory="Always" with "IfDifferent" so the file resets only when it has drifted, instead of copying on every build. ## Quick Start Ask the AI which CopyToOutputDirectory mode to use for a file that gets modified between builds and how to stop Always from copying on every build.

Frequently Asked Questions about copy-to-output-directory

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I choose between PreserveNewest, Always, and IfDifferent in MSBuild?

Use PreserveNewest for normal source files you edit, IfDifferent when the destination file gets mutated between builds and must be reset to the source, and Always only when you truly need a copy on every build. Never is the default for files not needed at runtime.

Why is my no-op build slow with CopyToOutputDirectory Always?

Always re-copies the file on every build, including otherwise-clean incremental builds, which is a measurable recurring cost. Switch to IfDifferent or set SkipUnchangedFilesOnCopyAlways to true so copies happen only when files actually differ.

What MSBuild version supports CopyToOutputDirectory IfDifferent?

IfDifferent and SkipUnchangedFilesOnCopyAlways require MSBuild 17.13 or later, which ships with .NET SDK 9.0.2xx and Visual Studio 2022 17.13. On older toolsets the value is not recognized and the item is silently not copied.

How do I reset a copied file that my tests modify between builds?

Set CopyToOutputDirectory to IfDifferent on the item. MSBuild compares timestamp and size, and re-copies the source over the mutated destination on the next build, restoring the known-good version without copying on every build.

Does SkipUnchangedFilesOnCopyAlways change Always behavior globally?

Yes. Setting SkipUnchangedFilesOnCopyAlways to true makes the Always copy target pass SkipUnchangedFiles to the Copy task, so items copy only when they differ. Set it in Directory.Build.props to opt an entire repo in at once.