I. Definition of resource files
Simply putProgramData that can be used, such as a string, image, or binary data, can actually contain any type of file.
Resource files can have multiple language versions, which is important when developing global applications. This feature uses the ResourceManager class in the system. Resources space to call that version. Set the attribute of a file to embedded resource in vs. Net to embed the resource into assembly.
Ii. What types of resource files are contained?
The system. Resources space supports three resource files :. A txt file can only contain string resources. Because it cannot be embedded into assembly, it is easy to expose and modified by the customer. The biggest drawback is that only string resources are supported, so it is not recommended. The resx file is composed of XML and can be added to any resource, including binary. Cannot be embedded into assembly. There is a dedicated read/write class in the system. Resources namespace. Vs. Net creates such a file, converts it to A. Resources file, and embeds it into Assembly according to the settings .. Resources file in PE format. You can add any resources. The only file that can be embedded into the Assembly has a dedicated read/write class in the system. Resources namespace.
3. How to call resource files?
The common practice is. TXT file. Resx files. The resources file is being called through ResourceManager, but we can also choose not to use that method. I think it is more troublesome, as shown below: we can directly embed a resource into the assembly without passing through a resource file. In vs. net, you can set the build attribute of a file to embedded resource. In this case, ResourceManager is useless because it can only obtain the. Resources resource file. So how to call such resources? We need to take advantage of some features in reflection. We only need to know some functions in the system. reflection. Assembly class. There are three related functions, but we only need the Assembly. getmanifestresourcestream function. This function returns a stream of resources embedded in the Assembly, and we can convert this stream into objects available in. net. For example, if the embedded resource is an image, we can use the bitmap constructor of the bitmap (Stream) to obtain the bitmap object of the image resource. If we embed a text file, we can use streamreader to read its content.
The following is a simple demo of using text files as resources.
Step 1: Resource file
Embed the file into the assembly.
Step 2: Call the resource file
Public stream getembeddedresource ()
{
Return System. reflection. Assembly. getexecutingassembly (). getmanifestresourcestream ("resourecedemo.test.txt"); // a key sentence
} // Resourecedemo.test.txt
// Resourecedemow.namespace, test.txt text file name
Private void button#click (Object sender, system. eventargs E)
{
Stream sresponse;
Streamreader srresponse;
Sresponse = getembeddedresource ();
Srresponse = new streamreader (sresponse );
String txtdemo = srresponse. readtoend (). tostring ();
Label1.text = txtdemo;
}
Of course, the same operation is performed in the image format. In the end, there is a tool that we should not forget: resgen.exe is used to convert resource file types. Conversion Between. txt <->. resx <->. Resources is supported.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/octverve/archive/2007/10/12/1822313.aspx