18

My company uses a combination of some database tables, a web page front end and an "export" application to handle our string resources in our web sites.

The export application used to work just fine when we used VS2008, but since switching to VS2010 the resources now have a designer.cs file "beneath" them in the solution explorer.

The problem is that the "export" application only generates the .resx files and not the underlying designer.cs files.

So, is there a way to not have those designer.cs files, or alternatively some way to automatically re-generate (or even some command the export application could call to re-generate them)

Antony Scott
  • 21,690
  • 12
  • 62
  • 94

5 Answers5

16

I had a problem where VS 2010 would not regenerate the Designer.cs files, and couldn't find the solution elsewhere.

I was able to regenerate them though, without going to the command line.

To fix the issue in Visual Studio 2010 I did the following:

  1. Deleted the Designer.cs file
  2. Right clicked on the main resx file
  3. Selected Run Custom Tool

That rebuilt the Designer.cs file.

Hope that might help someone else in the future..

ianneub
  • 357
  • 2
  • 8
  • 1
    of course, the Custom Tool property of the resx file should contain something like `PublicResXFileCodeGenerator` (and as soon as you edit that field, the designer file automatically gets generated) – ekkis Sep 19 '14 at 00:53
  • We use a custom tool to synchronise resx files from our language server. This tip is great for getting VS to rebuild the associated code-behind file! – Will Johnson Jul 02 '15 at 13:49
12

From MSDN we have:

Compiling Resources into Assemblies

When you build your application, Visual Studio invokes the resgen.exe tool to convert your application resources into an internal class called Resources. This class is contained in the Resources.Designer.cs file which is nested under the Resources.resx file in Solution Explorer. The Resources class encapsulates all your project resources into static readonly get properties as a way of providing strongly-typed resources at run-time. When you build through the Visual C# IDE, all the encapsulated resource data, including both the resources that were embedded into the .resx file and the linked files, is compiled directly into the application assembly (the .exe or .dll file). In other words, the Visual C# IDE always uses the /resource compiler option. If you build from the command line, you can specify the /linkresource compiler option that will enable you to deploy resources in a separate file from the main application assembly. This is an advanced scenario and is only necessary in certain rare situations.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • 1
    thanks, sometimes it's just knowing what to search for :) I will look into that on monday morning. – Antony Scott Jul 09 '10 at 23:56
  • 2
    fantastic, I just changed my export app to run the resgen tool if there was also a .designer.cs file (> 0 bytes) in the same folder as the .resx file. (I couldn't wait until Monday, curiosity got the better of me) – Antony Scott Jul 10 '10 at 21:10
  • 1
    hmm, this didn't quite work correctly. It compiled just fine but the application crashed when I ran it. So, I've tried a different approach which almost works. I've started a new question, because it's a slightly different problem. http://stackoverflow.com/questions/3230585/run-custom-tool-for-resx-files-in-mvc2-project-from-an-external-application-sc – Antony Scott Jul 12 '10 at 17:16
5

If you prefer to automatically generate the *.designer.cs files from *.resx files when building the project, the following approach worked for us and it might work for you as well:

  1. Close your solution
  2. Open as an XML file the project file in which you want to automatically generate the designer files. Note that you need to load it as an XML file. You can't edit these settings through the project property page.
  3. Add a target to the project as follows:
<Target Name="GenerateDesignerFiles">
   <Message Text="Deleting old Designer Files..."/>
   <Delete Files="@(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).resources')"/>
   <Delete Files="@(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).designer.cs')"/>
   <Message Text="Generating Designer Files..."/>
   <GenerateResource
      Sources="@(EmbeddedResource)"
      StronglyTypedLanguage="C#"
      StronglyTypedClassName="%(Filename)"
      StronglyTypedNamespace="@(EmbeddedResource->'%(CustomToolNamespace)')"
      StronglyTypedFileName="@(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).designer.cs')"
      PublicClass="true"
      >
   </GenerateResource>
   <Message Text="Generating Designer Files complete."/>
</Target>
  1. Locate the target named "BeforeBuild". This target may be commented out (the default).
  2. Modify the "BeforeBuild" target as follows:
<Target Name="BeforeBuild">
   <CallTarget Targets="GenerateDesignerFiles"/>
</Target>

This solution is based on all resource files being listed as "EmbeddedResource" within an ItemGroup of the project file, e.g.

<ItemGroup>
  <EmbeddedResource Include="Resources\Creditor\Display_Creditor.resx">
    <Generator>PublicResXFileCodeGenerator</Generator>
    <LastGenOutput>Display_Creditor.Designer.cs</LastGenOutput>
    <CustomToolNamespace>Acme.Web.Resources.Creditor</CustomToolNamespace>
  </EmbeddedResource>
  <EmbeddedResource Include="Resources\InboundEmail\Tooltip_InboundEmailDetails.resx">
    <Generator>PublicResXFileCodeGenerator</Generator>
    <LastGenOutput>Tooltip_InboundEmailDetails.Designer.cs</LastGenOutput>
    <CustomToolNamespace>Acme.Web.Resources.InboundEmail</CustomToolNamespace>
  </EmbeddedResource>
  <EmbeddedResource Include="Resources\Creditor\Tooltip_CreditorDetails.resx">
    <Generator>PublicResXFileCodeGenerator</Generator>
    <LastGenOutput>Tooltip_CreditorDetails.Designer.cs</LastGenOutput>
    <CustomToolNamespace>Acme.Web.Resources.Creditor</CustomToolNamespace>
  </EmbeddedResource>
</ItemGroup>

Disclaimer: This has been tested with Visual Studio 2013 and C# projects. It may or may not work for other projects and/or other versions of Visual Studio.

Manfred
  • 5,320
  • 3
  • 35
  • 29
  • It works with VS2015. Slight remark though: I had to provide the `CustomToolNamespace`, or else it would generate a `.cs` file named equal to the `.resx`-file. – Caramiriel Jan 25 '16 at 09:18
1

Try this:

  • Right click on resx file
  • Click on properties
  • Set the properties:
  • Copy to output Directory : Copy always
  • Custom tool : PublicResXFileCodeGenerator

Save and build again. Problem solved.

mmz
  • 49
  • 1
  • 1
  • 7
0

Following these steps worked for me.

  • Delete your designer.cs file.
  • Click on properties
  • Out put directory - copy always.
  • Custom tool: PublicResXFileCodeGenerator
  • Save and build.
  • Right click on resx and
  • Click run custom tool.
Hedego
  • 276
  • 2
  • 12