Do I need the resx Designer.cs file?
Asked Answered
H

5

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)

Haze answered 9/7, 2010 at 23:38 Comment(0)
E
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.

Eichman answered 9/7, 2010 at 23:43 Comment(3)
thanks, sometimes it's just knowing what to search for :) I will look into that on monday morning.Haze
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)Haze
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. #3231085Haze
J
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..

Jerk answered 7/5, 2013 at 21:19 Comment(2)
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)Mullinax
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!Recap
E
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.

Eichman answered 9/7, 2010 at 23:43 Comment(3)
thanks, sometimes it's just knowing what to search for :) I will look into that on monday morning.Haze
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)Haze
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. #3231085Haze
S
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.

Salmon answered 9/8, 2015 at 6:23 Comment(1)
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.Sidelong
R
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.

Roswald answered 9/3, 2018 at 8:48 Comment(1)
The steps you mentioned works after I have deleted the designer.cs file.Tatyanatau
T
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.
Tatyanatau answered 25/2, 2019 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.