Skip to main content
skill CX Codex

copy-to-output-directory — Claude Code skill: MSBuild copy modes

A skill that picks the right CopyToOutputDirectory mode in MSBuild: Never, PreserveNewest, Always or IfDifferent. Useful when files in bin/ go stale or builds slow down.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add dotnet/skills --skill copy-to-output-directory -a claude-code
Open on GitHub

What copy-to-output-directory does

The skill walks through four values of the CopyToOutputDirectory and CopyToPublishDirectory metadata. Never copies nothing, PreserveNewest copies only when the source is newer than the destination, Always copies on every build, and IfDifferent copies whenever the two differ in either direction — newer, older, or a different size.

The reason Always is usually the wrong pick is cost. It re-copies the file even in a no-op build, and on projects with large content that adds up. People used Always to reset a file that gets mutated between builds: an SQLite database, a state file, a config a test run edits. IfDifferent handles that case for less. The unchanged check is a heuristic on last-write timestamp and file size, not a content hash, so a destination edited to the same size and timestamp is not re-copied.

If a repo is already full of Always items, there is $(SkipUnchangedFilesOnCopyAlways). It makes the _CopyOutOfDateSourceItemsToOutputDirectoryAlways target skip files that have not changed. The property defaults to false for backwards compatibility and can be set in Directory.Build.props. Both IfDifferent and the property need MSBuild 17.13 or later (.NET SDK 9.0.2xx+, Visual Studio 2022 17.13+). On older toolsets the value is not recognized and the item is silently not copied — worth checking your SDK version before editing project files.

What it helps with

  • drop the per-build copy cost of Always items
  • reset a file that tests mutate between builds
  • pick a copy mode for project content
  • turn on skip-unchanged for a whole repository at once
  • find out why a file never lands in bin/ on an old SDK

Example prompts for the agent

  • Why is my no-op build not instant? Check for CopyToOutputDirectory="Always" in the project.
  • A test run mutates fixtures/catalog.db and it stays changed. How do I reset it to the source copy on each build?
  • Our repo has hundreds of Always items. Can I enable skip-unchanged through Directory.Build.props?

How to install copy-to-output-directory

1Skills CLI (Vercel)

npx skills add dotnet/skills --skill copy-to-output-directory -a claude-code

2Manually: put the files in place

~/.claude/skills/copy-to-output-directory/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/copy-to-output-directory/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about copy-to-output-directory

How do I install copy-to-output-directory in Claude?

Run npx skills add dotnet/skills --skill copy-to-output-directory -a claude-code. The skill lands in ~/.claude/skills/copy-to-output-directory/SKILL.md.

What does copy-to-output-directory do?

It helps you choose a CopyToOutputDirectory or CopyToPublishDirectory mode: Never, PreserveNewest, Always and IfDifferent. It explains when each one fits and why Always usually costs more than it is worth.

Which MSBuild version do I need for IfDifferent?

IfDifferent and $(SkipUnchangedFilesOnCopyAlways) require MSBuild 17.13 or later, meaning .NET SDK 9.0.2xx+ and Visual Studio 2022 17.13+. On older toolsets the value is not recognized and the item is not copied.

Source text

SKILL.md · MIT
---
name: copy-to-output-directory
description: "Choosing an MSBuild CopyToOutputDirectory / CopyToPublishDirectory mode: Never, PreserveNewest, Always, and IfDifferent (MSBuild 17.13+), plus $(SkipUnchangedFilesOnCopyAlways). USE FOR: removing the per-build Always copy perf hit; resetting output files mutated between builds. DO NOT USE FOR: general incremental-build diagnosis (use incremental-build); non-MSBuild build systems."
license: MIT
---

# Choosing a CopyToOutputDirectory Mode

## Overview

The `CopyToOutputDirectory` metadata (and its publish counterpart `CopyToPublishDirectory`) controls whether an item — `Content`, `None`, `EmbeddedResource`, or `Compile` — is copied next to your build output, and under what conditions the copy happens. Picking the wrong mode causes either stale files in `bin/` or an unnecessary per-build performance hit.

As of **MSBuild 17.13 / .NET SDK 9.0.2xx** there are four values:

| Mode | Copies when… | Incremental cost | Typical use |
| --- | --- | --- | --- |
| `Never` (default) | Never | None | Files not needed at runtime |
| `PreserveNewest` | Source is **newer** than destination (or destination missing) | Cheap (timestamp check) | The common case — source files you edit |
| `Always` | **Every build**, unconditionally | Expensive — copies on every build even in no-op builds | Legacy workaround; avoid (see below) |
| `IfDifferent` | Source **differs** from destination in either direction (newer **or** older, or size differs, or destination missing) | Cheap (timestamp + size check) | Destination may be mutated between builds |

```xml
<ItemGroup>
  <None Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
  <None Include="testdata\seed.db"  CopyToOutputDirectory="IfDifferent" />
</ItemGroup>
```

You can use either the attribute form shown above or the child-element form:

```xml
<None Include="testdata\seed.db">
  <CopyToOutputDirectory>IfDifferent</CopyToOutputDirectory>
</None>
```

## Why `Always` is usually the wrong choice

`Always` re-copies the file on **every** build, including otherwise-clean incremental/no-op builds. On projects with many or large content files this is a measurable, recurring cost and a common cause of "why is my no-op build not instant?" reports.

Historically `Always` was the only way to handle a specific scenario: **the destination file can change between builds** — for example an SQLite database, a storage/state file, or a config file that a test run mutates. With `PreserveNewest`, if the destination is modified (making its timestamp *newer* than the source) MSBuild will *not* restore the original source file, because the source is no longer newer. People reached for `Always` to force the file back into a known-good state — paying the copy cost on every build as a side effect.

## `IfDifferent`: copy when different, in either direction

`IfDifferent` is the targeted fix for that scenario. It copies the source over the destination whenever MSBuild considers the two **different** — whether the source is newer *or* older than the destination, whether the size differs, or the destination is missing — and skips the copy when the destination is unchanged per MSBuild's heuristic.

Under the hood the `_CopyDifferingSourceItemsToOutputDirectory` target uses the `Copy` task with `SkipUnchangedFiles="true"`. That "unchanged" check is a **heuristic**: it compares **last-write timestamp and file size only** — not a content hash — so a destination that was edited to the same size and timestamp as the source is treated as unchanged and is *not* re-copied. In practice this restores a mutated destination back to the source version on the next build (the reason people reached for `Always`) while avoiding the unconditional per-build copy.

Use `IfDifferent` when:

- A test run or the app itself writes to the copied file (databases, caches, state/storage files, editable config) and you want each build to reset it to the source version.
- You were using `Always` purely as a "keep the output in sync with the source" mechanism, not because you truly need a copy on every single build.

```xml
<ItemGroup>
  <!-- Reset the fixture DB to the source copy whenever it has drifted,
       but don't pay a copy on every no-op build. -->
  <None Include="fixtures\catalog.db" CopyToOutputDirectory="IfDifferent" />
</ItemGroup>
```

## Globally softening `Always` with `$(SkipUnchangedFilesOnCopyAlways)`

If you have an existing codebase full of `CopyToOutputDirectory="Always"` items and want the performance benefit without editing every item, set the property:

```xml
<PropertyGroup>
  <SkipUnchangedFilesOnCopyAlways>true</SkipUnchangedFilesOnCopyAlways>
</PropertyGroup>
```

This makes the `_CopyOutOfDateSourceItemsToOutputDirectoryAlways` target pass `SkipUnchangedFiles="true"` to its `Copy` task, so `Always` items are only copied when they actually differ — effectively giving `Always` the same skip-unchanged behavior as `IfDifferent`.

- Default is `false` for backwards compatibility (classic `Always` = copy every build).
- Set it in `Directory.Build.props` to opt an entire repo in at once.
- Prefer converting individual items to `IfDifferent` when you can; use this property when a bulk, non-invasive opt-in is more practical.

## How the modes flow through the build

`GetCopyToOutputDirectoryItems` buckets each item by its `CopyToOutputDirectory` value. Three copy targets then do the work as dependencies of `_CopySourceItemsToOutputDirectory` (which is itself invoked by `CopyFilesToOutputDirectory`):

- `_CopyOutOfDateSourceItemsToOutputDirectory` — `PreserveNewest` items (incremental via `Inputs`/`Outputs` timestamp comparison).
- `_CopyOutOfDateSourceItemsToOutputDirectoryAlways` — `Always` items (unconditional copy unless `$(SkipUnchangedFilesOnCopyAlways)` is `true`).
- `_CopyDifferingSourceItemsToOutputDirectory` — `IfDifferent` items (`SkipUnchangedFiles="true"`).

All copied files are registered in `FileWrites`, so `dotnet clean` removes them.

**Transitive copy:** items marked `Always`, `PreserveNewest`, or `IfDifferent` also flow to referencing projects through `ProjectReference` (via `_CopyToOutputDirectoryTransitiveItems`). `Never` items do not. `IfDifferent` participates in ClickOnce publish item collection alongside `Always`/`PreserveNewest`.

## Version requirement

`IfDifferent` and `$(SkipUnchangedFilesOnCopyAlways)` require **MSBuild 17.13 or later** (**.NET SDK 9.0.2xx+ / Visual Studio 2022 17.13+**). On older toolsets the value is not recognized: it will not match the `Always`/`PreserveNewest`/`IfDifferent` conditions in the common targets, so the item is silently **not copied**. Gate usage on the toolset if you must support older SDKs, or require the minimum SDK via `global.json`.

## Quick decision guide

- Don't need the file at runtime → `Never` (or omit — it's the default).
- Normal source file you edit → `PreserveNewest`.
- Destination gets mutated between builds and must be reset to the source → `IfDifferent`.
- You truly need a fresh copy on literally every build → `Always` (rare).
- Stuck with lots of legacy `Always` and want the perf win without edits → keep `Always` but set `$(SkipUnchangedFilesOnCopyAlways)=true`.

© dotnet · MIT · dotnet/skills

Similar skills

Everything for DevOps and infrastructure →

From the same set101

target-authoring A skill for writing custom MSBuild targets: canonical patterns plus fixes for anti-patterns like overwriting DependsOn chains. CX Codex +1 3,000installs template-comparison This skill puts two or more dotnet new templates side by side in a table and picks a winner for your scenario. Handy when you are stuck between webapi and webapp or blazor and blazorwasm. CX Codex +1 2,800installs template-smart-defaults A skill that fills related dotnet new parameters when you leave them unset, and leaves your explicit choices alone. CX Codex +1 2,800installs migrate-xunit-to-mstest Converts .NET test suites from xUnit.net v2 or v3 to MSTest v4 while preserving VSTest or MTP runners. Helpful when unifying test frameworks across .NET repositories. CX Codex +1 2,900installs create-datadriven-aspnetcore Generates ASP.NET Core CRUD code — Razor Pages, Blazor components, controllers, and Minimal APIs — aligned with project styles and EF Core without CLI generators. CX Codex +1 2,800installs scaffold-dotnet-test-project Creates or repairs a .NET test project: finds why CI discovers zero tests and wires the project into the solution file CI actually uses. CX Codex +1 2,600installs testability-obstacle Adds the smallest production seam in C#/.NET code so a test stops depending on real time, files or randomness. CX Codex +1 2,500installs vectorization Guides SIMD code design, implementation, and review in .NET projects. Useful when vectorizing scalar loops or auditing low-level vector memory safety. CX Codex +1 2,000installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor