打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#资源文件的使用方法

C#资源文件的使用方法


一、导言

microsoft的.net从2002年1月15日第一版发布到今天,已经得到了广泛的使用;从刚推出时国内相关书籍种类寥寥,到今天.net的相关书籍的大大丰富,已有越来越多的人——初学者或者有经验的程序员,在学习、应用.net。本文面向对.net有些了解的者,以c#为例,介绍如何在.net编程环境中,把资源文件(如包含图片、字符串等的资源文件)嵌入到程序集中。这里的所说的程序集可以是exe文件,也可是供其他程序调用的dll文件。



本文不涉及国际化、本地化、打包和部署资源方面的内容,有兴趣的读者可以查阅.net framework sdk文档。



二、软件环境

运行本文中的程序需要如下软件环境:windows 2000/xp, .net frameworksdk。本文中的代码在如下环境中运行通过:windows xp professional, .net framework v1.1 ,visual studio.net 2003。



三、资源文件

几乎每一个生产性应用程序都需要使用资源。资源是在逻辑上由应用程序部署的任何非可执行数据。资源可以在应用程序中作为错误信息显示,或者作为用户界面的一部分显示。资源可以包含多种形式的数据,包括字符串、图像和持久的对象。通过在资源文件中存储数据,无需重新编译整个应用程序即可更改数据。

在.net中,有文本文件.resx 文件和 .resources文件三种资源文件。如果资源将只包含字符串数据,则文本文件是最简单的选择。如果资源将包含对象或字符串与对象的组合,则必须创建 .resx 文件或.resources 文件。注意,只有 .resources 文件才应能嵌入在公共语言运行库程序集和附属程序集中。



四、创建资源文件

创建资源文件,有编写代码以及利用名为reseditor的软件这两种途径。以下分述之。



4.1 编写代码创建资源文件

.net framework 类库中提供了resourcewriter 类来创建.resources 文件。resourcewriter类包含在system.resources命名空间中。resourcewriter 类以系统默认的格式将资源写入输出文件或输出流。

在resourcewriter 类中使用 addresource 方法将资源指定为名称和值对。资源名在用于查找时是区分大小写的,但是,为更易于支持创作工具和帮助消除错误,resourcewriter 将不允许使用仅大小写不同 .resources 文件名。

若要创建一个资源文件,请用唯一的文件名创建 resourcewriter,至少调用 addresource 一次,再调用 generate 将该资源文件写入磁盘,然后调用 close 关闭该文件。

下面的示例将若干个字符串写入到 myresources.resources 文件中。

//例1

//本示例代码来自 .net framework sdk文档

//createrestest_1_1.cs

using system;

using system.resources;



public class writeresources {

public static void main(string[] args) {

// creates a resource writer.

iresourcewriter writer = new resourcewriter("myresources.resources");



// adds resources to the resource writer.

writer.addresource("string 1", "first string");

writer.addresource("string 2", "second string");

writer.addresource("string 3", "third string");

// writes the resources to the file or stream, and closes it.

writer.close();

}

}

编译代码:csc createrestest_1_1.cs , 编译成功后,则在工作目录里生成名为createrestest_1_1.exe的可执行文件;运行该文件,在工作目录中生成名为string.resources的资源文件。



以上给出了字符串写入资源文件的例子,下面的示例尝试将若干张图片嵌入到资源文件myresources.resources中。

//例2

//createrestest_1_2.cs

using system;

using system.drawing;

using system.resources;



public class creatpicresource

{

public static void main ()

{

// creates a resource writer.

resourcewriter rw = new resourcewriter ( "picture.resources" );



//从指定的文件创建image对象.

//_bird.png、 _butterfly.png文件在当前工作目录

image _bird_pic = image.fromfile ( "_bird.png" );

image _butterfly_pic = image.fromfile ("_butterfly.png" );



//把image对象添加到资源文件中

//resourcewritername.addresource(string name, object value);

//其中name为资源名,value为资源值

rw.addresource ( "bird" , _bird_pic );

rw.addresource ( "butterfly" , _butterfly_pic );



// writes the resources to the file or stream, and closes it.

rw.generate ();

rw.close ();

}

}



确保_bird.png和_butterfly.png文件在当前工作目录。编译代码:csc createrestest_1_2.cs ,如编译成功,生成createrestest_1_2.exe;运行该文件,则生成资源文件picture.resources。





4.2 利用资源编辑器 (reseditor)创建资源文件

.net framework 中包含一个称为 reseditor 的示例应用程序,它可帮助您创建和编辑资源文件。reseditor可以创建二进制资源文件 (.resources) 以及 xml 资源文件 (.resx)。



生成 reseditor

reseditor 以源代码的形式随 .net framework sdk 一起提供。必须先使用提供的批处理文件生成reseditor,然后才能使用它。找到\sdk\v1.1\samples\tutorials\resourcesandlocalization\reseditor文件夹,运行批处理文件build.bat,编译成功后,生成reseditor.exe应用程序。在笔者所用的环境中,路径如下:

\program files\microsoft visual studio .net 2003\sdk\v1.1\samples\tutorials\resourcesandlocalization\reseditor 。



生成 reseditor 后,您可以使用它创建、编辑资源文件。



使用 reseditor 创建资源文件



启动reseditor应用程序。

从“添加”下拉菜单中选择要添加的资源类型。

在“添加”文本框中键入资源的名称,然后单击“添加”按钮,将资源项添加到文件中。

在主窗格中,单击资源名称旁边的单元格以指定一个值。

对于“字符串”资源,在该框中键入相应的字符串。

对于“图像”和其他类型的资源,请浏览到相应的文件。

对于要添加到文件中的每个资源,重复步骤 3、4、5。

在“文件”菜单中,单击“另存为”以保存文件。您可以将文件保存为 .resources 文件,也可以保存为 .resx 文件。



编辑现有资源文件

可以使用 reseditor 编辑现有资源文件(.resources 文件和 .resx 文件)。使用方法如下:



启动reseditor应用程序。

在“文件”菜单上单击“打开”。

在“打开资源文件”对话框中浏览到相应的资源文件。

资源文件打开,并且它包含的资源显示在主窗格中。



如果要更改任何资源的值,请单击资源名称旁边的单元格并指定正确的值。

对于“字符串”资源,在该框中键入相应的字符串。

对于“图像”和其他类型的资源,请浏览到相应的文件。

如果要重命名资源,请执行以下操作:

通过单击要重命名的资源,突出显示它。

在“重命名”文本框中键入新名称。

单击“重命名”按钮,应用新名称。

如果要删除资源,请通过单击该资源将其突出显示,然后从“资源”菜单中选择“删除”。

编辑完资源文件后,选择“文件”,然后选择“另存为”以保存文件。





五、使用资源文件

创建了资源文件后,很容易将它们添加到您的应用程序中。二进制资源文件 (.resources) 或 xml 资源文件 (.resx)可直接添加到您的项目中。当编译项目时,同时也会编译资源文件。您可以通过使用 resourcemanager类检索嵌入的资源(即已经编译到程序集中的资源)。

如果您希望经常更新程序中的资源而无需重新编译整个解决方案,可创建一个资源程序集。



5.1 在命令行编译中使用资源文件

这里使用例2代码生成的资源文件picture.resources作为示例。示例代码createrestest_2_1.cs如下:



//例3

//createrestest_2_1.cs

using system;

using system.drawing;

using system.windows.forms;

using system.resources;

using system.reflection;



public class testresform : system.windows.forms.form

{

private picturebox picbox1;

private picturebox picbox2;



public testresform()

{

picbox1 = new picturebox();

picbox1.location = new point(0,0);

picbox1.width = rescontainer.instance.butterflyimage.width;

picbox1.height = rescontainer.instance.butterflyimage.height;

picbox1.image = rescontainer.instance.butterflyimage;



picbox2 = new picturebox();

picbox2.location = new point(0,100);

picbox2.width = rescontainer.instance.birdimage.width;

picbox2.height = rescontainer.instance.birdimage.height;

picbox2.image = rescontainer.instance.birdimage;



controls.add(picbox1);

controls.add(picbox2);

this.size = new size(200,200);

}



public static void main()

{

application.run(new testresform());

}

}



public class rescontainer

{

// data members

private image _birdimage = null;

private image _butterflyimage = null;

private static rescontainer _instance = new rescontainer();



// constructor

private rescontainer()

{

try

{

resourcemanager rm = new resourcemanager( "picture",

assembly.getexecutingassembly() ) ;

_butterflyimage = (image)( rm.getobject ( "butterfly" ) );

_birdimage = (image)( rm.getobject ( "bird" ) );

}

catch(exception ex)

{

ex.tostring();

}

}



// properties

public static rescontainer instance

{

get{

return _instance;

}

}



public image butterflyimage

{

get{

return _butterflyimage;

}

}



public image birdimage

{

get{

return _birdimage;

}

}

}

在控制台下,切换工作目录到当前代码、资源文件所在目录,运行csharp编译器csc(具体使用请参见.net frameworksdk)。确保资源文件存在且正确,输入:csc /t:winexe /resource:picture.resourcesuserestest_2_1.cs,编译成功后得到userestest_2_1.cs.exe。



5.2 在visual studio.net中使用资源文件

向您的项目添加资源文件:从“项目”菜单中,选择“添加现有项”。“添加现有项”对话框打开。浏览到要添加到项目中的资源文件。它可能是 .resources 文件,也可能是 .resx 文件。选择适当的文件。

在“生成”菜单中,选择“生成解决方案”将资源文件嵌入到您的已编译项目中。注意,如果对已添加到项目中的资源文件作出更改,需要在“生成”菜单中,选择“重新生成解决方案”,使更改生效。

至于代码,和userestest_2_1.cs略有不同。假设所在项目的命名空间为project1,则需要把rescontainer构造方法中的:

resourcemanager rm

= new resourcemanager( "picture", assembly.getexecutingassembly() ) ;

改为:
ResourceManager rm = new ResourceManager(" project1.picture",

Assembly.GetExecutingAssembly() ) ;

如果不加上命名空间,则生成解决方案时并不会出现任何错误提示,而在编译、执行会出现错误。奇怪的时执行完ResourceManager rm

= new ResourceManager( "picture", Assembly.GetExecutingAssembly() ) ; 这一行时rm并不为null,也不抛出异常,就这样悄悄的进行下去了。执行到下一句:

_ButterflyImage = (Image)( rm.GetObject ( "Butterfly" ) );

异常抛出,异常信息如下:“System.Resources.MissingManifestResourceException: Couldnot find any resources appropriate for the specified culture (or theneutral culture) in the given assembly. Make sure \"picture.resources\"was correctly embedded or linked into assembly \"Project1\"”。



究其原因,应该是IDE捣的鬼,把后添加如的资源文件也纳入其命名空间中。试把项目的默认命名空间删除(右键单击项目,属性,常规),则资源文件前就不需要加上命名空间了。

resgen工具:

resgen.exe   /useSourcePath   /compile   "d:\My   Document\My  Program\DirectoryCompare\DirectoryCompare\MainForm.resX","Debug\DirectoryCompare.MainForm.resources"


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在.Net中嵌入资源文件到程序集中
.NET的资源并不限于.resx文件,你可以采用任意存储形式[上篇]
.NET系统学习----Globalization & Resources
鸿蒙HarmonyOS应用开发之资源文件【鸿蒙开发17】
Android设置颜色的几种方法
YARNMRv2 Node Manager深入剖析—NodeManager启动Container流程分析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服