Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi!

To copy content files to an output folder, one could set the Copy to output directory property of a file to "Copy always".

Alternatively, one could add a post-build event:
XCOPY "$(ProjectDir)Templates" "$(TargetDir)" /E /I /F /Y
(or sth like that).

The problem is as following:
I often have errors related to data files which are read by my application. Usually they are related to a syntax, a missing semicolon etc. In such cases C# source code isn't changed anywhere and therefore VS does not recompile anything. Post-build events aren't fired and in result I have to push "rebuild [projectname]" to force copying content files. It is irritating, because:
1) sometimes I forgot about it and search for a non-existent error.
2) project is becoming bigger and bigger and a compilation takes time.

I know a quick-and-dirty solution -- manually run a batch script each time, but it wouldn't solve [1] problem.

=> Setting a Build Action to "None" doesn't make it.

Thanks

Greetings -- Jacek.
Posted
Updated 14-Nov-10 23:35pm
v2
Comments
wwwx 28-Feb-17 21:25pm    
I test and have to set the file Property > [Build Action] to [Content] then will work, if set [Build Action] as [Resource] seems not work

reference image
http://i.imgur.com/zwtVB3V.png

1 solution

If you add the file to the project, not the solution, and set the properties of the file to;
Build Action = None
Copy to Output Directory = Copy always

Setting the Build Action is the important part as it tells VS to ignore the outcome of the build and just copy the file.
Now when you Build the project, the file is copied to the output directory even if no code changes.

One last thing you could still use the XCOPY but make it a Pre-Build event for the project.
 
Share this answer
 
v2
Comments
Lutosław 15-Nov-10 5:34am    
Thanks for answering, bu it does not work. Test:
1. make an error, save, recompile. An exception is thrown. Good.
2. Correct error, save, just run. An exception is still thrown. Bad.
3. Recompile. An exception is not thrown. Good-Bad
Rod Kemp 15-Nov-10 5:46am    
Try Build Action = Content this should copy the file to the output directory before the app runs where as Build Action = None seems to copy the file after the app starts.
I think you are always going to have an issue with timing, as sometimes the app may start before the copy has finished.
As I originally said though if you click build and no code has changed then it will still copy the file even if the build for the project doesn't happen.
Lutosław 15-Nov-10 17:48pm    
Thanks for help.
I've already tried both 'None' and 'Content' build actions. It's not about timing either, because a problem is repeatable (if that'd be a reason then at the second run everything should go as expected, because updated files would be already copied). The files ARE NOT updated until I do rebuild. :?:
Rod Kemp 15-Nov-10 20:22pm    
That's odd, I tested it and the file was always copied across even if I just hit F5.
You can always try putting the XCOPY option into a pre-build event as this should always be run.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900