当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中资源文件的使用

asp.net中资源文件的使用

2017年12月12日  | 移动技术网IT编程  | 我要评论

funky forsest电影,江南医娘子,黑夜追风

其中,资源是的范围很广,它可由多种元素组成,包括与用户交互的界面元素(如位图、图标或光标)、应用程序所需数据的自定义文件以及安装 api 使用的版本文件、菜单和对话框等都可以作为资源。为.net程序集添加资源,就可实现资源重用等功能。使用visual studio.net集成开发环境ide很容易创建资源文件,把资源添加到工程中的方法和添加窗体、类库一样简单,只是你需要设置资源的“buildaction”属性为“embedded resource”,这样你就可以使用这些资源
创建资源
字符串表是极常见的一种资源。要创建这类资源文件,有以下两种方式:
(1)使用.net命令行工具resgen创建。首先创建包含资源内容的文本文件,可使用(记事本、editplus等文本编辑器)。该文本文件由所需要的“键值对”组成,键的名称可以在程序中引用,设置键名后把字符串值赋予该键即可完成文件的创建。作为示例,以下语句段产生这样的资源,按下面的格式保存为userinfo.txt文件:

复制代码 代码如下:

username="songh";
sex="boy";
birthday="1973-01-15";
salary="5000rmb";

然后,把文本文件转换为资源文件,这仍然通过resgen工具来实现。执行以下语句:resgen userinfo.txt,就将生成资源文件userinfo.resources。另外,resgen还可以创建基于xml格式的.resx资源文件,执行以下命令resgen userinfo.resources userinfo.resx 就将生成xml格式的资源userinfo.resx。不过,resgen工具不支持图象资源的操作,下面的方法就不具有这样的限制。
(2)使用resourcewriter类。 为易于创建资源文件,.net结构提供了resourcewriter类以支持图象等各种资源类型的创建。resourcewriter类包含的方法能以系统默认的格式将资源写入输出文件或输出流。与方法1)不同的是,这里统一在一个过程中完成。
要创建一个资源文件,请调用resourcewriter类的构造函数初始化类实例并至少提供流名或文件名。资源的实际内容通过调用addresource方法来完成,addresource方法将资源指定为名称和值对。资源的实际写入需要调用generate方法来实现,不过,在调用close方法关闭该resourcewriter时将隐式调用generate方法。
resourcewriter.addresource()方法向要写入资源的列表中添加资源。在创建resourcewriter类实例后,该方法可以添加至多2gb的资源,下面的重载方法之一用于向资源列表中添加string资源:
复制代码 代码如下:

public void addresource(
string name,//键名
string value//值
);

在这里,addresource方法的第一个参数指定键名称,第二个参数指定值。多次调用该方法就可以完成字符串表的创建。另外,添加图象资源可以通过实例化类image来实现(这时,请添加system.drawing名称空间)。
下面的代码段生成包含字符串表和图象的资源文件userinfo.resources。
复制代码 代码如下:

using system;
using system.resources;
using system.drawing;
public class rs
{
public static void main()
{
resourcewriter rw=new
resourcewriter("userinfo.resources");//提供文件名以初始化resourcewriter类实例。
image image=image.fromfile("photo.gif");//实例化image类
rw.addresource("photo",image);//添加图象
rw.addresource("username","songh");//添加字符串
rw.addresource("sex","boy");//添加字符串
rw.addresource("birthday","1973-01-15");//添加字符串
rw.addresource("salary","5000rmb");//添加字符串
rw.close();//关闭resourcewriter并隐式调用generate()方法完成资源文件写入磁盘文件。
}
}

上面的代码首先打开图形文件photo.gif,创建一个image对象。这样做时,这个图形文件必须存在于工程可执行文件的目录(通常是项目的\bin\debug目录)下,或者在image.fromfile()的方法参数中指定图象的完整路径。然后,通过几次调用addresouce()方法把字符串资源添加到resourcewriter对象中。最后,调用close()方法关闭resourcewriter对象并隐式调用generate()方法把资源写入文件userinfo.resources。
编译以上代码并运行就将创建资源文件userinfo.resources。
以上两种方式生成的资源文件均可以作为一个外部文件添加到程序集中,或者内嵌到dll或exe中。下面继续说明如何在windows应用程序使用资源文件。
使用资源文件
使用visual studio.net集成开发环境ide,可以把很容易把资源文件添加到程序集中。只需要在创建的工程中添加已经存在的资源文件,简单设置其属性就可将资源文件嵌入该程序集。下面通过一个c# windows控制台实例来说明任何使用上面创建的userinfo.resources资源文件。
首先,创建c# windows console项目resourceuserinfo,打开"项目\添加现有项",找到前面创建的资源文件userinfo.resources添加到这个工程中;
然后,选择这个资源文件,将属性buildaction(生成操作)设置为embedded resource(嵌入的资源),这样,资源文件就可以嵌入到输出的程序集中。
现在,你可以使用这个资源文件了。system.resources名称空间中的resourcemanager类提供在运行时方便地访问特定资源的途径。具体地可以通过getobject和getstring方法来实现,以键名为参数就将返回对应的值。
resourcemanager类的构造函数初始化resourcemanager类的新实例,其重载方法之一查找包含在一些文件中的资源,这些文件是使用给定的 assembly 从指定根名称导出的。
复制代码 代码如下:

public resourcemanager(
string basename,
assembly assembly
)

其中,参数basename表示资源的根名称。根名称由应用程序名称空间和资源文件名(不带扩展名)组成。这样,该例中资源的根名称应该是:userinforesource.userinfo,通过调用getmanifestresourcenames()方法也可编程获取该名称。
另一个参数assembly表示的是当前的主程序集,本例中的主程序集其实也是正在执行的程序集。获取正在执行程序集的一个简单方法是调用assembly.getexecutingassembly()方法。
在获取resourcemanager实例后,通过指定键名,就可以获得对应的资源。
下表是程序中使用的部分控件:
类别 textbox textbox textbox textbox picturebox
名称 username sex birthday salary photo
这些控件均可直接从工具箱拖放到设计器中。
完整的源代码为:
方法一:
复制代码 代码如下:

using system.reflection;
using system.resources;
private system.resources.resourcemanager rm;
public form1()
{
initializecomponent();
assembly assembly=assembly.getexecutingassembly();//获取当前主程序集
rm=new resourcemanager("resourceuserinfo.userinfo",assembly);//实例化资源管理类
photo.iamge=(image)rm.getobjetct("photo");
username.text=rm.getstring("username");
sex.text=rm.getstring("sex");
birthday.text=rm.getstring("birthday");
salary.text=rm.getstring("salary");
}

方法二:
复制代码 代码如下:

assembly assm = this.gettype().assembly;//assembly.loadfrom(程序集路径);
foreach (string resname in assm.getmanifestresourcenames())
{
stream stream = assm.getmanifestresourcestream(resname);
resourcereader rr = new resourcereader(stream);
idictionaryenumerator enumerator = rr.getenumerator();
while (enumerator.movenext())
{
dictionaryentry de = (dictionaryentry)enumerator.current;
//de.key是资源名
//de.value是资源内容
}
}

运行以上代码,便可取出资源文件内容。
posted @ 2011-12-15 11:40 tasting 阅读(21) 评论(0) 编辑
dodragdrop 方法的使用
dodragdrop方法,用于开始对象的拖放操作。
在类库中的定义为:
复制代码 代码如下:

[uipermissionattribute(securityaction.demand, clipboard = uipermissionclipboard.ownclipboard)]
public dragdropeffects dodragdrop(
object data,
dragdropeffects allowedeffects
)

  其中data参数为要拖放的数据,如果拖动操作需要于另一个进程的应用程序相互操作,data代表的数据应该是基本托管类(string,bitmap,或metafile),或者是实现 iserializable 或idataobject的对象。 allowedeffects参数表示拖放的效果,为一个枚举值(dragdropeffects).返回值也为dragdropeffects枚举值。
  当开始调用dodragdrop方法拖动一个数据对象时,dodragdrops在拖放过程中,检测当前光标位置下的控件是不是有效的放置目标。如果当前光标下的控件是有效的放置目标,则givefeedback事件以指定的拖放效果引发。在检测当前位置光标是否为有效的拖放目标时,dodragdrops方法同时跟踪光标位置,键盘状态和鼠标状态的更改。
   (1)如果用于移出了一个窗口,则引发dragleave事件。
  (2)如果移入了另外一个控件,则引发该控件的dragenter事件。
  (3)如果鼠标移动,但是停留在一个控件中,则引发dragover事件。
如果检测到更改了键盘或者鼠标状态,则引发拖放源的querycontinuedrag事件, 并根据事件的querycontinuedrageventargs的action属性值确定继续拖动,放置数据或取消操作。
(1)如果action属性指定为continue,则将引发dragover事件。
(2)如果action属性指定为drop,则将放置效果返回给源,以便应用程序对数据进行适当的操作;例如,如果是移动操作,则剪切数据。
(3)如果是dragaction的值为cancel,则引发dragleave事件
从csdn上摘抄一段示例代码:
  下面的代码示例演示在两个 listbox 控件之间的拖放操作。当拖动动作启动时,该示例调用 dodragdrop 方法。在 mousedown 事件期间,如果从鼠标位置起鼠标移动的距离大于 systeminformation..::.dragsize,则启动拖动动作。indexfrompoint 方法用于确定在 mousedown 事件期间要拖动的项的索引。
  该示例还演示如何对拖放操作使用自定义光标。该示例要求应用程序目录中存在两个光标文件:3dwarro.cur 和 3dwno.cur,分别用于自定义拖动光标和禁止停放光标。如果选中 usecustomcursorscheckcheckbox,则使用自定义光标。自定义光标在 givefeedback 事件处理程序中设置。
  键盘状态在右 listbox 的 dragover 事件处理程序中计算,以确定基于 shift、ctrl、alt 或 ctrl+alt 键的状态将发生哪种拖动操作。放置动作在 listbox 中发生的位置也在 dragover 事件期间确定。如果要放置的数据不是 string,则 dragdropeffects 中将把 drageventargs.seffect 设置为 none。最后,停放状态在 droplocationlabellabel 中显示。
  要放置的用于右 listbox 的数据在 dragdrop 事件处理程序中确定,并且在 listbox 中的适当位置添加该 string 值。如果拖动操作移动到窗体边框的外面,则 querycontinuedrag 事件处理程序中将取消拖放操作
复制代码 代码如下:

using system;
using system.drawing;
using system.windows.forms;
namespace snip_dragndrop
{
public class form1 : system.windows.forms.form
{
private system.windows.forms.listbox listdragsource;
private system.windows.forms.listbox listdragtarget;
private system.windows.forms.checkbox usecustomcursorscheck;
private system.windows.forms.label droplocationlabel;
private int indexofitemundermousetodrag;
private int indexofitemundermousetodrop;
private rectangle dragboxfrommousedown;
private point screenoffset;
private cursor mynodropcursor;
private cursor mynormalcursor;
/// the main entry point for the application.
[stathread]
static void main()
{
application.run(new form1());
}
public form1()
{
this.listdragsource = new system.windows.forms.listbox();
this.listdragtarget = new system.windows.forms.listbox();
this.usecustomcursorscheck = new system.windows.forms.checkbox();
this.droplocationlabel = new system.windows.forms.label();
this.suspendlayout();
// listdragsource
this.listdragsource.items.addrange(new object[] {"one", "two", "three", "four",
"five", "six", "seven", "eight",
"nine", "ten"});
this.listdragsource.location = new system.drawing.point(10, 17);
this.listdragsource.size = new system.drawing.size(120, 225);
this.listdragsource.mousedown += new system.windows.forms.mouseeventhandler(this.listdragsource_mousedown);
this.listdragsource.querycontinuedrag += new system.windows.forms.querycontinuedrageventhandler(this.listdragsource_querycontinuedrag);
this.listdragsource.mouseup += new system.windows.forms.mouseeventhandler(this.listdragsource_mouseup);
this.listdragsource.mousemove += new system.windows.forms.mouseeventhandler(this.listdragsource_mousemove);
this.listdragsource.givefeedback += new system.windows.forms.givefeedbackeventhandler(this.listdragsource_givefeedback);
// listdragtarget
this.listdragtarget.allowdrop = true;
this.listdragtarget.location = new system.drawing.point(154, 17);
this.listdragtarget.size = new system.drawing.size(120, 225);
this.listdragtarget.dragover += new system.windows.forms.drageventhandler(this.listdragtarget_dragover);
this.listdragtarget.dragdrop += new system.windows.forms.drageventhandler(this.listdragtarget_dragdrop);
this.listdragtarget.dragenter += new system.windows.forms.drageventhandler(this.listdragtarget_dragenter);
this.listdragtarget.dragleave += new system.eventhandler(this.listdragtarget_dragleave);
// usecustomcursorscheck
this.usecustomcursorscheck.location = new system.drawing.point(10, 243);
this.usecustomcursorscheck.size = new system.drawing.size(137, 24);
this.usecustomcursorscheck.text = "use custom cursors";
// droplocationlabel
this.droplocationlabel.location = new system.drawing.point(154, 245);
this.droplocationlabel.size = new system.drawing.size(137, 24);
this.droplocationlabel.text = "none";
// form1
this.clientsize = new system.drawing.size(292, 270);
this.controls.addrange(new system.windows.forms.control[] {this.listdragsource,
this.listdragtarget, this.usecustomcursorscheck,
this.droplocationlabel});
this.text = "drag-and-drop example";
this.resumelayout(false);
}
private void listdragsource_mousedown(object sender, system.windows.forms.mouseeventargs e)
{
// get the index of the item the mouse is below.
indexofitemundermousetodrag = listdragsource.indexfrompoint(e.x, e.y);
if (indexofitemundermousetodrag != listbox.nomatches) {
// remember the point where the mouse down occurred. the dragsize indicates
// the size that the mouse can move before a drag event should be started.
size dragsize = systeminformation.dragsize;
// create a rectangle using the dragsize, with the mouse position being
// at the center of the rectangle.
dragboxfrommousedown = new rectangle(new point(e.x - (dragsize.width /2),
e.y - (dragsize.height /2)), dragsize);
} else
// reset the rectangle if the mouse is not over an item in the listbox.
dragboxfrommousedown = rectangle.empty;
}
private void listdragsource_mouseup(object sender, system.windows.forms.mouseeventargs e) {
// reset the drag rectangle when the mouse button is raised.
dragboxfrommousedown = rectangle.empty;
}
private void listdragsource_mousemove(object sender, system.windows.forms.mouseeventargs e)
{
if ((e.button & mousebuttons.left) == mousebuttons.left) {
// if the mouse moves outside the rectangle, start the drag.
if (dragboxfrommousedown != rectangle.empty &&
!dragboxfrommousedown.contains(e.x, e.y)) {
// create custom cursors for the drag-and-drop operation.
try {
mynormalcursor = new cursor("3dwarro.cur");
mynodropcursor = new cursor("3dwno.cur");
} catch {
// an error occurred while attempting to load the cursors, so use
// standard cursors.
usecustomcursorscheck.checked = false;
}finally {
// the screenoffset is used to account for any desktop bands
// that may be at the top or left side of the screen when
// determining when to cancel the drag drop operation.
screenoffset = systeminformation.workingarea.location;
// proceed with the drag-and-drop, passing in the list item.
dragdropeffects dropeffect = listdragsource.dodragdrop(listdragsource.items[indexofitemundermousetodrag], dragdropeffects.all | dragdropeffects.link);
// if the drag operation was a move then remove the item.
if (dropeffect == dragdropeffects.move) {
listdragsource.items.removeat(indexofitemundermousetodrag);
// selects the previous item in the list as long as the list has an item.
if (indexofitemundermousetodrag > 0)
listdragsource.selectedindex = indexofitemundermousetodrag -1;
else if (listdragsource.items.count > 0)
// selects the first item.
listdragsource.selectedindex =0;
}
// dispose of the cursors since they are no longer needed.
if (mynormalcursor != null)
mynormalcursor.dispose();
if (mynodropcursor != null)
mynodropcursor.dispose();
}
}
}
}
private void listdragsource_givefeedback(object sender, system.windows.forms.givefeedbackeventargs e)
{
// use custom cursors if the check box is checked.
if (usecustomcursorscheck.checked) {
// sets the custom cursor based upon the effect.
e.usedefaultcursors = false;
if ((e.effect & dragdropeffects.move) == dragdropeffects.move)
cursor.current = mynormalcursor;
else
cursor.current = mynodropcursor;
}
}
private void listdragtarget_dragover(object sender, system.windows.forms.drageventargs e)
{
// determine whether string data exists in the drop data. if not, then
// the drop effect reflects that the drop cannot occur.
if (!e.data.getdatapresent(typeof(system.string))) {
e.effect = dragdropeffects.none;
droplocationlabel.text = "none - no string data.";
return;
}
// set the effect based upon the keystate.
if ((e.keystate & (8+32)) == (8+32) &&
(e.allowedeffect & dragdropeffects.link) == dragdropeffects.link) {
// keystate 8 + 32 = ctl + alt
// link drag-and-drop effect.
e.effect = dragdropeffects.link;
} else if ((e.keystate & 32) == 32 &&
(e.allowedeffect & dragdropeffects.link) == dragdropeffects.link) {
// alt keystate for link.
e.effect = dragdropeffects.link;
} else if ((e.keystate & 4) == 4 &&
(e.allowedeffect & dragdropeffects.move) == dragdropeffects.move) {
// shift keystate for move.
e.effect = dragdropeffects.move;
} else if ((e.keystate & 8) == 8 &&
(e.allowedeffect & dragdropeffects.copy) == dragdropeffects.copy) {
// ctl keystate for copy.
e.effect = dragdropeffects.copy;
} else if ((e.allowedeffect & dragdropeffects.move) == dragdropeffects.move) {
// by default, the drop action should be move, if allowed.
e.effect = dragdropeffects.move;
} else
e.effect = dragdropeffects.none;
// get the index of the item the mouse is below.
// the mouse locations are relative to the screen, so they must be
// converted to client coordinates.
indexofitemundermousetodrop =
listdragtarget.indexfrompoint(listdragtarget.pointtoclient(new point(e.x, e.y)));
// updates the label text.
if (indexofitemundermousetodrop != listbox.nomatches){
droplocationlabel.text = "drops before item #" + (indexofitemundermousetodrop + 1);
} else
droplocationlabel.text = "drops at the end.";
}
private void listdragtarget_dragdrop(object sender, system.windows.forms.drageventargs e)
{
// ensure that the list item index is contained in the data.
if (e.data.getdatapresent(typeof(system.string))) {
object item = (object)e.data.getdata(typeof(system.string));
// perform drag-and-drop, depending upon the effect.
if (e.effect == dragdropeffects.copy ||
e.effect == dragdropeffects.move) {
// insert the item.
if (indexofitemundermousetodrop != listbox.nomatches)
listdragtarget.items.insert(indexofitemundermousetodrop, item);
else
listdragtarget.items.add(item);
}
}
// reset the label text.
droplocationlabel.text = "none";
}
private void listdragsource_querycontinuedrag(object sender, system.windows.forms.querycontinuedrageventargs e) {
// cancel the drag if the mouse moves off the form.
listbox lb = sender as listbox;
if (lb != null) {
form f = lb.findform();
// cancel the drag if the mouse moves off the form. the screenoffset
// takes into account any desktop bands that may be at the top or left
// side of the screen.
if (((control.mouseposition.x - screenoffset.x) < f.desktopbounds.left) ||
((control.mouseposition.x - screenoffset.x) > f.desktopbounds.right) ||
((control.mouseposition.y - screenoffset.y) < f.desktopbounds.top) ||
((control.mouseposition.y - screenoffset.y) > f.desktopbounds.bottom)) {
e.action = dragaction.cancel;
}
}
}
private void listdragtarget_dragenter(object sender, system.windows.forms.drageventargs e) {
// reset the label text.
droplocationlabel.text = "none";
}
private void listdragtarget_dragleave(object sender, system.eventargs e) {
// reset the label text.
droplocationlabel.text = "none";
}
}
}

对用这种拖放操作和微软的服务,容器模式的关系,留在以后再学习。
posted @ 2011-12-15 11:16 tasting 阅读(92) 评论(0) 编辑
关于接口的使用
概述: 接口的使用体现了一种泛化的思想。
应用场景之一是:
(1)多个类都要实现某些动作,而该动作具体实现的方式又不一样。
如对于创建条件来的窗体来说,要实现添加整形参数,bool类型参数和字符串类型参数的条件添加的窗体。而在具体的实现整形和字符串的窗体,以及bool类型的添加的窗体过程中,实现细节又不一样。
复制代码 代码如下:

public interface iexpressionform
{
conditionitementity cientity { get; set; }
conditionbranchentity cientity { get; set; }
event handle onexpressionhandled; }

  而对于每个窗体来说,都需要包含cientity,cientity属性和onexpressionhandled添加条件后的事件,因此在iexpressionform 接口中定义了cientity,cientity,和onexpressionhandled。
接着实现整形参数的条件添加窗体
复制代码 代码如下:

public partial class frmnumericcondition : form, iexpressionform
{
public frmnumericcondition()
{
initializecomponent();
}
public conditionitementity cientity { get; set; }
public conditionbranchentity cbentity { get; set; }
public event handle onexpressionhandled;
}

  然后是字符型参数条件添加窗体
复制代码 代码如下:

public partial class frmvarcharcondition : form, iexpressionform
{
public frmvarcharcondition()
{
initializecomponent();
}
public conditionitementity cientity { get; set; }
public conditionbranchentity cbentity { get; set; }
public event handle onexpressionhandled;
}

以此类推,实现其它参数类型条件添加的窗体。
那我这样实现的目的的好处是什么呢?接下来我们来看看,我定义的一个产生窗体的函数
复制代码 代码如下:

public static iexpressionform createexpressionform(conditiontype ct)
{
iexpressionform frm = null;
if (ct == conditiontype.bit)
frm = new frmbitcondition();
else if (ct == conditiontype.datetime)
frm = new frmdatetimecondition();
else if (ct == conditiontype.numeric)
frm = new frmnumericcondition();
else if (ct == conditiontype.varchar)
frm = new frmvarcharcondition();
return frm;
}

从定义中我们可以看出,返回值类型为iexpressionform ,是我在上边定义的接口。因此该函数可以返回一切实现了iexpressionform 接口的类。如frmvarcharcondition 和frmnumericcondition.
这样就简单的实现了工厂模式,程序可以用很好的扩展性。
以上只是接口的应用场景之一,也是自己在写代码的时候发现的。写的不好。但也要写,一方面是要总结工作和学习,在总结的时候可以思考和发现,也希望能给阅读文章的人一些帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网