I want to create a Visual Studio (I am using VSTS 2008) project which simply does file copy work, in more details, I will add some files to this project and this project copy files (included in this project) to some destination location when I build the project.
Any ideas how to do this in VSTS?
BTW: I heard using proj file could do such kinds of task, but I have not found any good simple to learn samples for beginner. :-)
thanks in advance, George
The other answers to this question tend to assume you are not using VisualStudio but MSBuild directly or have an existing project for which you can add a custom build step or a build event (I have found build event type of solutions error prone because they will not always copy the files when you need them copied).
If you want a VisualStudio project that only does a copy, you can do this with a stock VisualStudio with a Makefile project.
The slightly misnamed Makefile project can do anything you can do from the command line. Obviously this means you can create a Makefile project that only copies files. In a Makefile project's Properties⇒Configuration Properties⇒NMake section are fields for Build, Rebuild, and Clean actions. These can be populated with plain old batch file commands. On every build, VisualStudio will create a temporary batch file from the proper field and execute it with cmd. Warning you will get the 32-bit cmd interpreter on 64-bit machines so you will need to use the magical sysnative directory if you want access 64-bit windows commands or DLLs (I'm not sure if this is documented anywhere so it may not always be the case - your batch scripts should be robust against the bit-ness of cmd changing.
Example batch code requiring 64-bit system files without knowing beforehand what cmd or OS (32-bit or 64-bit) will run the code:
(thie "copy foobar" line above will fail causing the build to fail.)
Note, VisualStudio (and MSBuild) will always run a Makefile project - it does not check the timestamp on the output file but instead punts that job to what it expects to be some other build tool (nmake, cmake, make, ant, maven, etc.). Since, in this case there is just a simple copy command and not some program checking whether or not to perform the copy, the copy occurs on every build.
VisualStudio always running makefile projects also means that every time you press F5 to debug your project, VisualStudio will annoy you with a poorly worded pop up a dialog box (if you haven't told it not to) telling you that you your project is out of date and that you must build it again. If you tell VisualStudio not to display the dialog, it will just perform the copy without asking before running the executable.
As mentioned before, Visual Studio .xxproj files are actually MSBuild files. So you can do in them whatever MSBuild allows them to do. I've used them to customize my build process quite a bit. In your case, what you're looking for is the Copy Task. You can add it in the AfterBuild target.
In Visual Studio 2010, you use Custom Build tools
For example, this rule copies "faq.txt" to the output directory.
You can add a post build step in Visual Studio:
There you can add the post build event commands, for example
copy $(ProjectDir)\bin\* SOME_OTHER_FOLDER\*
The $(ProjectDir) is a macro, there are a few more available, they will be shown, when you edit a command.
Then, if you have a look at the according project file (XYZ.csproj or XYZ.vbproj), you can see a property group added at the bottom of the file, such as:
This is how you would do it when directly editing an MSBuild file. Note that you don't need to launch Visual Studio in order to build and have your files copied, you can just pass the project file to the msbuild.exe.
The C#/VB.NET project files are MSBuild scripts. MSBuild can do whatever you need...