copy-to-output-directory
Ce qu’il fait
Cette skill guide le choix de la façon dont les fichiers projet sont copiés dans la sortie de build MSBuild. Elle s'adresse aux développeurs dont les builds sont lents parce que les fichiers sont copiés à chaque build, ou dont le dossier de sortie garde des fichiers obsolètes.
Quand l’utiliser
- ✓Quand un build est lent parce que des fichiers sont copiés à chaque build.
- ✓Quand des fichiers de sortie sont obsolètes ou ne se mettent pas à jour après une modification.
- ✓Quand vous devez choisir entre les modes de copie pour les fichiers de contenu ou de données.
Lien du skill
https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/copy-to-output-directory/SKILL.mdCopiez-le et collez-le dans votre IA (Claude, ChatGPT ou Cursor) en lui demandant de suivre les instructions.
Comment l’utiliser
Le plus simple : copiez le message et collez-le dans votre IA. Elle lit le skill et suit les instructions.
Message prêt à coller
Pour Claude Code, Codex ou Cursor : lancez-la dans le dossier du projet et le skill est installé.
Téléchargez le SKILL.md et placez-le dans le dossier de skills de votre agent.
Exemple de demande à votre IA
“Mon build copie un gros fichier de données de test à chaque fois, même quand rien n'a changé. Choisis le bon réglage de copie.”
5 nouveaux skills par semaine. 1 minute de lecture.
Comment ça marche
L'agent examine comment chaque fichier est utilisé à l'exécution et recommande le mode de copie adapté, comme conserver le plus récent ou copier seulement si différent.
Contenu technique (SKILL.md original)
Texte original de l’auteur, en anglais, directement depuis GitHub.
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 |
<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:
<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
Alwayspurely as a "keep the output in sync with the source" mechanism, not because you truly need a copy on every single build.
<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:
<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-uncha
Questions fréquentes
Quels sont les modes de copie ?
Ce sont Never, PreserveNewest, Always et IfDifferent, ce dernier étant disponible dans les SDK plus récents.
Pourquoi éviter Always ?
Always copie à chaque build, même sans changement, ce qui ralentit les builds incrémentaux.
Couvre-t-elle la sortie de publication ?
Oui. Elle couvre aussi le réglage de copie pour la publication.
Détails
Description originale de l’auteur
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.
repo: dotnet/skills
Popularité
- Étoiles GitHub
- 5.8k
- Position au classement
- #14596
- Licence
- MIT
- Mis à jour
- 02/11/2026
Commentaires
- …
Autres skills de ce dépôt
create-custom-agent
5.8kAide votre agent à créer des fichiers d'agents personnalisés pour VS Code, avec leurs propres outils, instructions et transferts vers d'autres agents.
create-skill
5.8kAide votre agent à créer une nouvelle skill d'agent avec un nom valide, une description de déclenchement claire et un fichier SKILL.md conforme à la norme.
csharp-scripts
5.8kAide votre agent à exécuter un petit programme C# en un seul fichier avec l'outil en ligne de commande .NET, sans créer de projet complet.
dotnet-pinvoke
5.8kAide votre agent à appeler correctement des bibliothèques natives C ou C++ depuis .NET, en couvrant les signatures, la gestion des chaînes, la mémoire et les handles sûrs.
Skills similaires
openclaw-update
394kAide votre agent à mettre à jour une passerelle OpenClaw existante, en choisissant la méthode adaptée à son installation et en gardant votre session active.
diagnose-gateway
394kAide votre agent à trouver pourquoi votre passerelle OpenClaw échoue, en vérifiant les paramètres, les canaux et les ports sans rien modifier.
crabbox-apps
394kAide votre agent à ouvrir et exécuter une application sur une machine cloud temporaire, puis à afficher sa vue dans le panneau latéral de votre chat.