How to: Build a Project That Has Resources
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
If you are building localized versions of a project, all user interface elements must be separated into resource files for the different languages. If the project uses only strings, the resource files can use text files. Alternatively, you can use .resx files as the resource files.
The library of common tasks that is provided with MSBuild includes a
GenerateResource task that you can use to compile resources in either .resx or text files. This task includes the
Sources parameter to specify which resource files to compile and the
OutputResources parameter to specify names for the output resource files. For more information on the
GenerateResource task, see GenerateResource Task.
Identify the project's resource files and pass them to the
GenerateResourcetask, either as item lists, or as file names.
OutputResourcesparameter of the
GenerateResourcetask, which allows you to set the names for the output resource files.
Outputelement of the task to store the value of the
OutputResourcesparameter in an item.
Use the item created from the
Outputelement as an input into another task.
The following code example shows how the
Output element specifies that the
OutputResources attribute of the
GenerateResource task will contain the compiled resource files
beta.resources and that those two files will be placed inside the
Resources item list. By identifying those .resources files as a collection of items of the same name, you can easily use them as inputs for another task, such as the Csc task.
This task is equivalent to using the /compile switch for Resgen.exe:
Resgen.exe /compile alpha.resx,alpha.resources /compile beta.txt,beta.resources
<GenerateResource Sources="alpha.resx; beta.txt" OutputResources="alpha.resources; beta.resources"> <Output TaskParameter="OutputResources" ItemName="Resources"/> </GenerateResource>
The following example project contains two tasks: the
GenerateResource task to compile resources and the
Csc task to compile both the source code files and the compiled resources files. The resource files compiled by the
GenerateResource task are stored in the
Resources item and passed to the
<Project DefaultTargets = "Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" > <Target Name="Resources"> <GenerateResource Sources="alpha.resx; beta.txt" OutputResources="alpha.resources; beta.resources"> <Output TaskParameter="OutputResources" ItemName="Resources"/> </GenerateResource> </Target> <Target Name="Build" DependsOnTargets="Resources"> <Csc Sources="hello.cs" Resources="@(Resources)" OutputAssembly="hello.exe"/> </Target> </Project>