What is target file ?

projects use .target files to define which files should be imported into the project during the build process (more info).

How to use it ?

1.in order to use target files firstly you need to tell your Visual C# project to use this future by adding below line to the project file (.csproj).

2.Then you need to import the targetFile address as below

 my target file name is “TargetFileApp.csproj.targets

3.In the next step you will ask the compiler when to do the target files task

A target never build twice but you can order the build by using  BeforeTargets and AfterTargets (read more) .

by above line it goes to my target file and looking for “CopyPackageContent” element.

4.now you can add the your desirable tasks to your target file

In the ItemsGroup you can define your addresses and filter them with Exclude attribute . for instance in this case, it is looking for all the files which exist in Test folder except the one has .etc extension.

when the project is build the compiler find the “CopypackageContent” element in the target file and then start copying from pre defined addresses to destination address (more info about copy task) .

download the whole source of TargetFileApp from my GitHub.

added : I also make a simple application to generate the target file for CSharp project.