Written by: Stanko Milosev
Category: C#
Hits: 10288

Copy if newer doesn't work as I (or probably anyone else) would expect.

For example create new WPF project (CopyIfNewerHell):

Then, in solution add new Class Library:

Now, create an XML, like:

<?xml version="1.0" encoding="UTF-8"?>
<rootNode>
	<childNode>
	  Child
	</childNode>
</rootNode>

Include that XML in the class library. On the end your solution should like something like:

Set properties of your XML:

Build action = Content, Copy to Output Directory = Copy if newer:

After that, add class library to CopyIfNewerHell project, so your references shoud look like:

Now you can hit F5. If you check bin folder, in my case it is here:

\Documents\Visual Studio 2012\Projects\CopyIfNewerHell\CopyIfNewerHell\bin\Debug\CopyIfNewerHell.xml

You will see CopyIfNewerHell.xml file. Open it in, for example, Notepad, and you will see:

<?xml version="1.0" encoding="UTF-8"?>
<rootNode>
	<childNode>
	  Child
	</childNode>
</rootNode>

Now in Visual studio, open your xml, and change it, like:

<?xml version="1.0" encoding="UTF-8"?>
<rootNode>
	<childNode>
	  ChildNode
	</childNode>
</rootNode>

Hit F5, and again if you open \Documents\Visual Studio 2012\Projects\CopyIfNewerHell\CopyIfNewerHell\bin\Debug\CopyIfNewerHell.xml in notepad, you will see that file was not changed. File CopyIfNewerHell.xml will be changed only if you change any property of that file. Also, if you change property Build action instead of Content to Embedded Resource then XML will be copied everytime when you change content of your file...

Solution I've found here

---

Here I explained another possible solution.