Running PublicResXFileCodeGenerator at compile time

Tim picture Tim · Jun 24, 2009 · Viewed 8.6k times · Source

We are using PublicResXFileCodeGenerator to generate *.Designer.cs files from *.resx files. Currently these are generated whenever the .resx is saved or when we right click and choose "Run Custom Tool".

I want to be able to run this tool automatically at build time, rather than having to do it manually. The Designer.cs files are not kept under source control, so it gets annoying to have to manually generate them every time someone else changes the resource, or on the first checkout. It's all right if the files are generated on every build, since they aren't very large, but doing it only when they're out of date would be even cooler.

Note: This would be part of a Visual Studio build, not a command line build. I found an answer to another question saying that it is possible from within VS, just not command line: Run a Visual Studio custom tool at build time

Answer

Simon Mourier picture Simon Mourier · Oct 14, 2011

You could write a VS Macro that simply runs the custom tool when you need it. The automation method is VSProjectItem.RunCustomTool. Have a look at this answer on SO for a similar problem: Run Custom Tool for entire solution

Otherwise, PublicResXFileCodeGenerator is just a VS wrapper on the StronglyTypedResourceBuilder Class, that you can automate the way you want. I don't know if there is an Msbuild Task on top of it, but you also could write one, or you can just write a custom program in a pre build step. Here is an example on how to use this class: Strongly typed resources in ASP.NET