Using Resource file for collection of strings
-
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
-
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
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 theResourceManager
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
-
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 theResourceManager
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
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
-
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
Read about the
ResourceManager
class in the .NET Framework. I already gave you a "walk-through" of how to use it. The class documentation forResourceManager
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 associatedTypeConverter
likeSize
,Point
, enumerations, etc.).Microsoft MVP, Visual C# My Articles