Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using Resource file for collection of strings

Using Resource file for collection of strings

Scheduled Pinned Locked Moved C#
csharphelptutoriallearning
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Inpreet Singh
    wrote on last edited by
    #1

    Hello, I am working in C#. Wanna use Resource files for storing message box strings in it. Then call required string using their specific names. Can anybody tell me how to create these files i tried to do the same but didn't manage to. Thanks for ur help. Inpreet Singh

    H 1 Reply Last reply
    0
    • I Inpreet Singh

      Hello, I am working in C#. Wanna use Resource files for storing message box strings in it. Then call required string using their specific names. Can anybody tell me how to create these files i tried to do the same but didn't manage to. Thanks for ur help. Inpreet Singh

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      If you're talking about ResX files (or even .txt files can be used), you should see Resources in Applications[^] in the .NET Framework. In VS.NET, this is pretty easy. With each form or control, VS.NET creates a hidden .resx file with the same name (only .cs.resx instead of .cs). If you click the "Show Hidden Files" button when your project is selected, you can see and edit these. They will be compiled into .resources files automatically. You can easily reference these in your class using something like:

      ResourceManager resources = new ResourceManager(this.GetType());

      ...where this is an instance of your class. There's other overloads for the constructor as well. Note that the ResourceManager will be instantiated for each instance of each class that uses it. A better way is often to have a single class that has an associated ResX file and use a singleton pattern. This is common in all the .NET FCL assemblies and I and others I've seen use this in larger applications as well. If you're building your project using the SDK and the command line, you use the resgen.exe utility to compile a .txt or .resx file into a .resources file with the fully-qualified name of the Type to which you want to associate it (btw, it doesn't have to be associated with a Type), such as MyNamespace.MyClass.resources to be associated with a Type MyNamespace.MyClass. When you compile these, you include all the .resources files as embedded resources (using the /res switch on csc.exe). Read that section on resources in applications, though, and you should get a better idea.

      Microsoft MVP, Visual C# My Articles

      I 1 Reply Last reply
      0
      • H Heath Stewart

        If you're talking about ResX files (or even .txt files can be used), you should see Resources in Applications[^] in the .NET Framework. In VS.NET, this is pretty easy. With each form or control, VS.NET creates a hidden .resx file with the same name (only .cs.resx instead of .cs). If you click the "Show Hidden Files" button when your project is selected, you can see and edit these. They will be compiled into .resources files automatically. You can easily reference these in your class using something like:

        ResourceManager resources = new ResourceManager(this.GetType());

        ...where this is an instance of your class. There's other overloads for the constructor as well. Note that the ResourceManager will be instantiated for each instance of each class that uses it. A better way is often to have a single class that has an associated ResX file and use a singleton pattern. This is common in all the .NET FCL assemblies and I and others I've seen use this in larger applications as well. If you're building your project using the SDK and the command line, you use the resgen.exe utility to compile a .txt or .resx file into a .resources file with the fully-qualified name of the Type to which you want to associate it (btw, it doesn't have to be associated with a Type), such as MyNamespace.MyClass.resources to be associated with a Type MyNamespace.MyClass. When you compile these, you include all the .resources files as embedded resources (using the /res switch on csc.exe). Read that section on resources in applications, though, and you should get a better idea.

        Microsoft MVP, Visual C# My Articles

        I Offline
        I Offline
        Inpreet Singh
        wrote on last edited by
        #3

        Hi, I want to use resource files to store message box strings. I already read that I can, by defining Name = value pair in text file. like ex > Close = "Really wann quit this application". But now the problem is where should I wirte this text file and then where should I compile Resgen myResource.txt , to create myResource.resource file. Now after doing all this how should I use these Name of string in my application to call resource file. Thanx for ur support. Inpreet Singh

        H 1 Reply Last reply
        0
        • I Inpreet Singh

          Hi, I want to use resource files to store message box strings. I already read that I can, by defining Name = value pair in text file. like ex > Close = "Really wann quit this application". But now the problem is where should I wirte this text file and then where should I compile Resgen myResource.txt , to create myResource.resource file. Now after doing all this how should I use these Name of string in my application to call resource file. Thanx for ur support. Inpreet Singh

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Read about the ResourceManager class in the .NET Framework. I already gave you a "walk-through" of how to use it. The class documentation for ResourceManager even includes an example. BTW, it's better to use ResX files (just XML documents) because you can specify a Type so that you can serialize more than just string (you can serialize anything with an associated TypeConverter like Size, Point, enumerations, etc.).

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups