如何使用C#使用资源文件 [英] How to use resource file using c#

查看:270
本文介绍了如何使用C#使用资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在基于 Windows 的电子学习应用程序上工作.我想使用资源来开发多语言支持.我对如何使用资源有疑问.我想使用字符串资源,但是它将适用于多种语言.

敬请帮助.

谢谢&问候,
Balkrishna


I am working on a Windows based E-learning application. i want to develop a multilingual support using resources. I have an issues with how to use resources. i am suppose to use the string resource, but will it work for multiple languages.

kindly help on the same.

Thanks & Regards,
Balkrishna

推荐答案

This[^] page links some excellent resources: The link "excellent 13 minute video" is particularly good, I use it to introduce my students to the topic.


要在C#中使用资源文件,您需要首先将资源文件(* .resx)包含到您的项目中.在VS.Net中,您只需在项目名称上单击鼠标右键,然后添加添加新项"或添加现有项"(如果尚未创建.resx文件)即可,否则,在添加新项"之后选择程序集资源文件"即可. .给您的资源文件起一个名字,例如. MyresourceFile.resx,然后单击确定.
现在,您可以在资源文件中添加特定于您的需求的项目.
在项目中拥有资源文件后,您需要添加以下名称空间:

使用System.Globalization;
使用System.Reflection;
使用System.Resources;

在此之后,声明Reourcesmanager类型的变量,如下所示:

私有System.Resources.ResourceManager RM =新的System.Resources.ResourceManager("MyresourceFile",System.Reflection.Assembly.GetExecutingAssembly());

现在,您可以使用GetObject和GetString方法轻松检索特定的对象和字符串.

lblMyImage.Image =(Image)rm.GetObject("MyPicture.png");//如果您将图片存储在资源文件中
this.lblResource.Text = rm.GetString("MyResourceName");//某些文本属性

您可以使用资源文件在您的网站上实现多语言网站或本地化.
To use a resource file in C# you need to first include the resource file(*.resx) into your project. In VS.Net you can do it simply by right clicking your project-name and going to Add a New item or Add a existing item if you had already created .resx file if not then after going to Add a New Item select Assembly Resource file. Give a name to your resource file for eg. MyresourceFile.resx and click on OK.
Now you can add item specific to your requirement in the the resource file.
Once you have resource file available in your project you need to add following namespaces :

using System.Globalisation;
using System.Reflection;
using System.Resources;

After this declare variable of type Reourcesmanager as follows:

private System.Resources.ResourceManager RM = new System.Resources.ResourceManager(" MyresourceFile",System.Reflection.Assembly.GetExecutingAssembly());

Now you can easily retrieve your specific objects and strings by using the GetObject and GetString methods.

lblMyImage.Image = (Image)rm.GetObject("MyPicture.png");// if you picture stored in your resource file
this.lblResource.Text = rm.GetString("MyResourceName");//Some Text attributes

You can use the Resource file for implemeting Multilingual site or Localisation in your web site.


这篇关于如何使用C#使用资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆