Related Operations on C # resource files

Source: Internet
Author: User

Related operations on resource files.

1. Compare common file streams that have access to the resource files, and then convert to the corresponding file

A more typical practice is to load a specified resource through a code assembly

The Assembly is obtained by assembly static Method GetExecutingAssembly () as follows

There are many ways to get code assemblies

System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly ();

Stream manifestresourcestream = null;

Manifestresourcestream = asm. GetManifestResourceStream ("mylibrary." + filename);

return manifestresourcestream;

Gets the file stream, which can also be a character stream.

If our resource file corresponds to this list is a picture file

System.Drawing.Bitmap Bitmap = new System.Drawing.Bitmap (Manifestresourcestream, true);

If our resource file is a cursor file

cursor cursor = new cursor (manifestresourcestream);

Icon

Icon icon = new icon (manifestresourcestream);

String

System.IO.StreamReader sr = new System.IO.StreamReader (manifestresourcestream);

String str = Sr. ReadLine ();

2. Another way of writing, mainly through the GetObject () method of the ResourceManager class

You can also perform an operation after you get the stream through GetStream ().

System.Resources.ResourceManager Manager =

New System.Resources.ResourceManager ("Resource Name", assembly.getexecutingassembly ());

Object target = Manager. GetObject ("Resource file name");//Gets the value of the specified resource

Here corresponds to the list we added to the resource file,

Converting back to my target value, which may result in a unboxing process

If our resource file corresponds to this list is a picture file System.Drawing.Bitmap Bitmap = (System.Drawing.Bitmap) target;

If our resource file is a cursor file

cursor cursor = (cursor) target;

Icon

Icon icon = (icon) target;

String

String str = target. ToString ();//or Manager

Manager. GetString ("Resource file name");

With both of these operations, you can load a specified resource from a resource file.

The premise is that your resource list has that resource.

It is recommended to use this method, which is also a method of manipulating resource files in Ms ResX.

Now that we can read the resource file, we start writing the resource file operation.

3. Of course, you can add a resource file directly through the design interface,

Remember to change the build action to "Embedded Resource", Embed

System.Resources.ResourceWriter writer =

New System.Resources.ResourceWriter ("dongpad.com.resources");

Writer. AddResource ("Bitmap", Image.FromFile (@ "Dongpad.bitmap"));

Writer. AddResource ("Cursor", Image.FromFile (@ "dongpad.cursor"));

Writer. AddResource ("ico", Image.FromFile (@ "Dongpad.ico"));

Writer. AddResource ("str", Image.FromFile (@ "dongpad.str"));

The name of the resource here remember to be unique.

Writer. Generate ();

Writer. Close ();

Next time, let's summarize some of the commonly used overrides in C # development. I hope you all support!

--Reference documents--------------------------------------------------------------------------

What is a resource file
Three steps, Globalization,localizability, and Localization are required to prepare the World-ready program in. NET. In this third step, Localization is the most common place to use resource files. (This article does not discuss the World-ready program, perhaps later in another article) because the logic interface of the program needs to be isolated from the resource interface, and the resource interface is what we call the resource file. As the name implies, a resource file, of course, is all resources, but what is a resource? The so-called resource is the data available in the program, such as strings, images, and any binary data, including any type of file. Note that a resource file can be available in multiple languages, for example, a strings.resources file can be in English, Simplified Chinese, and traditional Chinese. ResourceManager can automatically confirm which version is called based on the file name. Different versions simply add a regional language to the file name. For example, our strings.resources is the default version, the English version can be strings.en-us.resources (U.S. English), Simplified Chinese can be Strings.zh-chs.resources (Simplified Chinese), and traditional Chinese can be strings.zh-cht.resources (traditional Chinese). The so-called default version is the resource that is used when the appropriate resource version is not found, usually in English. The default file should be embedded in the main Assembly so that no resource-finding errors will occur. Setting the property of a file in vs.net to Embedded Resource allows the resource to be embedded in the master Assembly.
  
Resource file type
The System.Resources namespaces support three resource files:
. txt files, can only have string resources. Because it cannot be embedded in the Assembly, it is easily exposed and modified by the customer. The biggest drawback is that only string resources are supported, so it is not recommended.
. resx files, which are made up of XML, can be added to any resource, including binary. Also cannot be embedded in the Assembly. There is a dedicated read-write class in the System.Resources namespace. Vs.net creates the file and then turns it into a. resources file and embeds it into the Assembly according to the settings.
. resources file, PE format, can join any resource. The only file that can be embedded in the Assembly has a dedicated read-write class in the System.Resources namespace.
  
Several ways to invoke resource files
ResourceManager can return different local resources based on different UICulture settings (this is related to the World-ready program, not discussed here), we just need to know that the calling resource is used. Let's look at how to invoke each of the following:
. txt files:
cannot be called directly, it must be converted to a. resources file before it can be used. (see "Recommended Tools" for how to convert)
. resx files:
Can be read with ResXResourceReader, but this method is not intuitive, it is not recommended to call the. resx file directly. The correct way is to convert it into a. resources file, and then use ResourceManager as a read job. Note if the. resx files are added in Vs.net, they are automatically set to Embedded Resource and are embedded in the Assembly after they are transferred to the. resources file.
. resources file:
is divided into two situations:
be embedded or compiled into satellite Assembly:
Use ResourceManager's various constructor to get the resources in Assembly.
Individual files, not compiled or embedded in Assembly:
You can use ResourceManager.CreateFileBasedResourceManager to get the resource set (ResourceSet), which is all the resources.
Special cases:
Another special case is when you embed a resource directly, that is, to embed a resource directly into the Assembly without a resource file. This can be implemented in vs.net by setting the Build property of a file to Embedded Resource. In this case ResourceManager is useless because it can only get the. Resources resource file (in or not in Assembly). So how do you invoke this kind of resource? Not hard, we need to take advantage of some of the features in Reflection. Don't be afraid, not to let you learn Reflection, in fact, we just need to know some System.Reflection.Assembly this class of some functions can be. There are three related functions, but we only need to assembly.getmanifestresourcestream this function. This function returns a resource embedded in the Assembly as a stream, and we can turn the stream into an object available in. NET. For example, if the embedded resource is a picture, then we can take advantage of the New Bitmap (Stream) Bitmap constructor to get the Bitmap object for this image resource.
Note: Here are just a few ways to get a different resource, and see the documentation on how to use each class and function.
  
How to define exactly the logical location of a resource file
I think this is the most concern of many people! Here the author will explain how to correctly fill in the Resoucemanager (string, Assembly) This constructor, and how to correctly fill in Assembly.GetManifestResourceStream (string ), because the principle of their two is the same. Having seen the description above, it's a lot easier to get here. The main discussion here is how to fill out the String. This String is the full name of the resource, and a full name consists of its namespace and the first part of the file name (BaseName). For example, if the default namespace (root namespace) is DefaultNamespace, the name of the resource file is Strings.en-us.resources, then its full name is defaultnamespace.strings. This is very simple, but how to determine the name space? This is a bit odd, because the C # compiler is somewhat different from the vb.net compiler. Here, the author gives the two compilers how to automatically add namespaces to embedded resources:
C#
It automatically adds the default namespace (same as root namespace), but also adds the name of the subfolder. For example, the resource file Strings.en-us.resources in the subfolder sub-folder, itsthe full name isDefault namespace + subfolder + base name = DefaultNamespace.Subfolder.Strings
vb.net
In vb.net it is simple, it automatically adds root namespace to the embedded resource. Regardless of which subfolder you place the resource file in, the full name of the resource file is always root namespace + base name.
According to the above description, if we use C #, using Vs.net to add a resource file called Images.resources in the NewFolder subfolder, then we should use the following code to obtain these resources, assuming that the default namespace is Myde Fault
ResourceManager res = new ResourceManager ("MyDefault.NewFolder.Images", this. GetTy

Related Operations on C # resource files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.