If you create managed application in Visual Studio 2005, you will notice that a resource class is generated when you add resources to the resx file. The class makes getting resource values much easier. But some are troubled because the class is generated as Internal, which blocks you to access it in another assembly. For people who create one assembly specially for resources, it could be a problem.
It is a limitation of the current code generator within VS. However, the command line tool resgen.exe (in the Framework SDK) does support you to create a public class from your resource file.
We want to do is to disable the default code generator in the VS. To do that, we need show all hidden files in the solution explorer (from the toolbar). Select the resx file, and clear the “Custom Tool” property of this item. However, if we are using VB, for the default resource.resx under “My Project”, the resource designer will add “Custom Tool” back when we open it. We would have to create a new resource file to the project, and do not use the default resource file.
Now, after editing the resx file, we can bring up a command window, and run the resgen.exe with property option, we will get the generated code, and of course, need to add it to the project. It will be pain to do that everytime you edit the file. However, we can put this in the project file, so it will be done automatically when we build the project.
Just save the project, and open the project file in notepad. Edit the file carefully (and backup one in case we mess it up.) Just add those to project file (It is a sample with csproj, we need change some for VB):
<Target Name=”BeforeBuild” DependsOnTargets=”CreateStrongTypeResource”/>
<Target Name=”CreateStrongTypeResource” DependsOnTargets=”GenStrongTypeResource” >
<Output TaskParameter=”Include” ItemName=”Compile” />
<Target Name=”GenStrongTypeResource” Inputs=”Properties\Resources.resx” Outputs=”Properties\Resources.cs”>
<Exec Command=”"$(SdkPath)\bin\resGen.exe" /str:c# /publicClass Properties\Resources.resx”/>
Save the csproj file, and reload it in the Visual Studio. Now, we can change the resx file with the new resource designer in VS2005, and build it, the generated type will be built into the assembly.