Use resource files in ASP. NET

Source: Internet
Author: User

You can create a resource file in Visual Web Developer 2005 express edition. You can store strings, images, icons, text files, and other content in the file.. net.

The resource file extension is. resx. In the form software, you can use resxresourcereader to directly read The. resx file. However, in ASP. NET, we cannot do this. We need to use resgen.exe to generate a binary. Resources file using. resx (note the extension), and then use resourcereader to read the file. Note: resxresourcereader and resourcereader.

Create. resx

Creating a. resx file in Visual Web Developer 2005 express edition is very simple. You can select this type of file in the "New File" dialog box. You can also create a file by using resourcewriter.

Generate. Resources

We need to install.. NET Framework. Then, you can find the resgen.exe tool at a location similar to c: \ Program Files \ Microsoft Visual Studio 8 \ SDK \ V2.0 \ bin. the resx file is compiled. resources file.

Resgen E: \ resource1.resx E: \ resource1.resources

Read. Resources

Use resourcereader.

The namespace of resourcereader is:

System. Resources

Example

Resourcereader reader = new resourcereader (server. mappath ("resource1.resources "));

String resourcetype;
Byte [] resourcedata;
Reader. getresourcedata ("string1", out resourcetype, out resourcedata );
Byte [] arr = new byte [resourcedata. Length-1];
For (INT I = 0; I <arr. length; I ++)
{
Arr [I] = resourcedata [I + 1];
}
LB. Text = encoding. utf8.getstring (ARR );

System. Collections. idictionaryenumerator en = reader. getenumerator ();
While (EN. movenext ())
{
LB. Text + = "<br/>" + en. value. tostring ();
}

Reader. Close ();

The sample code contains four sections. The first section is the creation of resourcereader, and the fourth section is the release of resourcereader resources. For details about how to release resources by close, see the differences and usage of finalize (destructor), dispose, and close.

The second and third sections are two read methods.

The first parameter of getresourcedata is resourcename case sensitive. encoding. utf8.getstring converts the byte array into a string. I don't know why the byte read from getresourcedata always has one more byte, so I used array conversion to remove the previous byte.

In the third section, use getenumerator to retrieve all resources and display them separately.

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.