Sunday, September 18, 2005

Assemblies Notes

  • Creating Resource Files:

    • Creating Resx Files in VS.NET (embedding resources):

      1. Right-click on the project to which you want to add a Resx file and click on Add\Add New Item.
      2. Choose Assembly Resource File.
      3. Click on Open.
      4. Select the data in the left pane and then click on the name column in the right pane.
      5. Enter a name.
      6. Enter a value.
      Note that this method embeds the resource file within the current project's assembly file. In a way, the assembly is a resource assembly; although you can use the project's assembly for more than just serving resources. Upon compilation, the contents of the resource file are embedded into the project's assembly MSIL.

      To access the resources via the same assembly: 1) Create an instance of ResourceManager 2) pass the constructor the following parmaters: a) "Namespace.ResourceFileNameNoExtension" and b) this.GetType().Assembly. 3) retrieve the resource itself via the ResourceManager.GetString() or ResourceManager.GetObject() method: // Namespace = Test1 // Resource file = TestResource.resx ResourceManager rm = new ResourceManager("Test1.TestResource", this.GetType().Assembly); this.txtTest.Text = rm.GetString("test");
    • Creating Resource Assemblies:

      Creating a Resx resource assembly is pretty much the same as that outlined in Creating Resx Files in VS.NET (embedding resources) above with the exception being the embedded resource is not part of the current project (the project accessing the resource(s)).

      • To create an assembly resource that uses a Resx file, use the steps outlined in Creating Resx Files in VS.NET (embedding resources).

        To access the resources of the resource assembly: 1) Create an instance of ResourceManager 2) pass the constructor the following parmaters: a) "ResourceAssemblyNamespace.ResourceFileNameNoExtension" and b) this.GetType().Assembly. 3) retrieve the resource itself via the ResourceManager.GetString() or ResourceManager.GetObject() method: // Namespace = ResourceAssembly // Resource file = TestResource.resx ResourceManager rm = new ResourceManager("ResourceAssembly.TestResource", Assembly.LoadFile(Application.StartupPath + "\\ResourceAssembly.dll")); this.txtTest.Text = rm.GetString("test");
      • To create an assembly resource that uses a resources (binary) file: Use the steps outlined above, only instead of adding a Resx file, create a .resources file using the ResEditor utility. You should be able to find this utility in "%PROGRAMFILES%\Microsoft Visual Studio .NET 2003\SDK\v1.1\Samples\Tutorials\resourcesandlocalization\reseditor".

        To access the resources of the resource assembly: perform the same steps as above.