文谷首页 | 业界传真 | 网络技术 | 服务器 | 数据库 | 存储技术 | 系统安全 | 无线技术 | Cisco | .Net | Windows | Linux | Unix | Java
电子商务 | 网站工程 | 网页设计 | 平面设计 | 多媒体 | 编程语言 | Oracle | MSSQL | Photoshop | ASP | PHP | 实用技巧 | 进程查询 | 文谷论坛
 websphere   .net framework
您现在的位置: IT文谷 >> 开发平台 >> .net framework >> .NET Framework >> 文章正文
Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础
Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础
Microsoft .NET 框架资源基础

Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础

   摘要:Chris Sells 讨论无类型清单资源和有类型资源,它们是受 Microsoft .NET 框架支持的两种资源。他定义了这两种资源,并介绍了如何在您自己的应用程序中使用它们。下载 winforms02202003.exe 示例文件。

假设要在应用程序中通过从文件加载位图来设置窗体的背景图像:

public Form1() {

  ...

  // Load a file from the file system

  this.BackgroundImage =

    new Bitmap(@"C:\WINDOWS\Web\Wallpaper\Azul.jpg");

}

   该代码的问题是,并非所有 Microsoft Windows 的安装实例都有 Azul.jpg,即使是那些确实具有该文件的安装实例,该文件可能也不在安装实例的相同位置。即使您与应用程序一起交付该图片,节省空间的用户也可能决定删除它,这会导致您的应用程序出错。确保图片或任何文件与代码在一起的唯一安全方式是将它作为资源嵌入并加载。

清单资源

资源是在编译时添加到程序集中的。例如,如果您使用命令行编译器,则可以使用 /resource 开关嵌入资源:

C:\>csc.exe myApp.cs /resource:c:\windows\web\wallpaper\Azul.jpg

   /resource 开关将文件作为资源嵌入,嵌入时使用文件名(没有路径)作为资源名称。文件嵌入到程序集的清单 资源集中。程序集的清单由一组作为程序集一部分的元数据组成。该元数据的一部分是与每个嵌入资源关联的名称和数据。执行 ildasm 时,可以在清单部分看见程序集清单资源的列表,如图 1 所示。

C:\>ildasm.exe myApp.exe

winforms02202003-fig01

图 1. ildasm 显示嵌入资源

  可以像 ildasm 一样枚举清单资源的列表,这需要使用 system.reflection.assembly 类的 getmanifestresourcenames 方法:

using System.Reflection;

...

// Get this type's assembly

Assembly assem = this.GetType().Assembly;



// Enumerate the assembly's manifest resources

foreach( string resourceName in assem.GetManifestResourceNames() ) {

  MessageBox.Show(resourceName);

}

   一旦通过枚举清单资源或硬编码一个您想要的清单资源而知道了清单资源的名称,就可以通过 assembly 类的 getmanifestresourcestream 方法将该清单资源作为原始字节流进行加载,如下所示:

using System.IO;



public Form1() {

  ...



  // Get this type's assembly

  Assembly assem = this.GetType().Assembly;



  // Get the stream that holds the resource

  // NOTE1: Make sure not to close this stream!

  // NOTE2: Also be very careful to match the case

  //        on the resource name itself

  Stream stream =

    assem.GetManifestResourceStream("Azul.jpg");



  // Load the bitmap from the stream

  this.BackgroundImage = new Bitmap(stream);

}

   因为资源可以像类型名称一样有冲突,所以最好用资源自己的“命名空间”来嵌入资源,该操作可以使用 /resource 开关的扩展格式来完成:

C:\>csc myApp.cs /resource:c:\...\azul.jpg,ResourcesApp.Azul.jpg

   注意在要嵌入的文件名的逗号后面使用的备用资源名称。备用资源名称允许您为资源任意地提供时间嵌套名称,不管文件名是什么。它是设置在程序集中的备用名称,如图 2 所示。

 

winforms02202003-fig02


图 2. 使用备用名称的嵌入资源

下面是使用备用名称的更新后的资源加载代码:

public Form1() {

  ...



  // Get this type's assembly

  Assembly assem = this.GetType().Assembly;



  // Load a resource with an alternate name

  Stream stream =

    assem.GetManifestResourceStream("ResourcesApp.Azul.jpg");



  // Load the bitmap from the stream

  this.BackgroundImage = new Bitmap(stream);

}

   为了更方便,如果您的资源和加载资源的类碰巧使用了相同的命名空间,则可以将类的类型作为可选的第一参数传递给 getmanifestresourcestream:

namespace ResourcesApp {

  public class Form1 : Form {

    public Form1() {

      ...



      // Get this type's assembly

      Assembly assem = this.GetType().Assembly;



      // Load the resource using a namespace

      // Will load resource named "ResourcesApp.Azul.jpg"

      Stream stream =

        assem.GetManifestResourceStream(this.GetType(), "Azul.jpg");



      // Load the bitmap from the stream

      this.BackgroundImage = new Bitmap(stream);

    }

    ...

  }

}

GetManifestResourceStream 将使用如下格式编写资源名称:

<namespace>.<fileName>

在加载某些类型(比如 bitmap 类)时,使用类型和文件名也是有用的,这样可以通过提供构造函数避免由您自己打开流:

namespace ResourcesApp {
public class Form1 : Form { public Form1() {
... // Get this type's assembly Assembly assem = this.GetType().Assembly;
// Load the bitmap directly from the manifest resources this.BackgroundImage = new Bitmap(this.GetType(), "Azul.jpg");
} ... }}

 

[1] [2] [3] 下一页  

[1] [2] [3] 下一页  

Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础


  • 上一篇文章:

  • 下一篇文章:
  • 进入论坛讨论

    相关文章
    基于.NET的多线程编程入门
    VS2005常用插件搜罗
    .net中用GDI+提高gif图片保存画质
    VS.NET:通过Web服务瞄准电子商务
    对象设计中创建VS使用
    安装/初始化/个性化DotNetNuke
    介绍dotnet原动力(DNN)
    GDI+编程10个基本技巧
    DTE .NET工程的自动化编译
    .Net中如何操作IIS的原理分析
    VS.NETRC5无法创建Add-IN工程
    .NET中带有口令加密的注册页面
    热门文章最新推荐

    版权与免责声明:
    ① 本网转载其他媒体稿件是为传播更多的信息,此类稿件不代表本网观点,版权归原作者所有,本网不承担此类稿件侵权行为的连带责任。
    ② 在本网BBS上发表言论者,文责自负。
    ③ 如您因版权等问题需要与本网联络,请在30日内联系 。
    Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础
    Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础2006-7-15Microsoft .NET 框架资源基础

    .net framework专题
    人气排行
  • 此栏目下没有文章
  • 最近更新
    普通文章技术介绍 新数据网格简介
    普通文章.NET 开发人员该下载的十个必备
    普通文章深入剖析Asp.net资源文件
    普通文章通过事例学习.net的WebForms技
    普通文章ASP.NET Tips1---合并多个字段
    普通文章微软明年1月份出台.NET专业开发
    普通文章微软下月底前完成Vista大部分功
    普通文章揭开微软 .NET 认证考试的面纱
    普通文章Visual Studio:Microsoft开发工
    普通文章Visual Studio:Microsoft开发工
    全站热点       
    最新推荐
    关于文谷 | 联系文谷 | 免责声明 | 文谷社区
    Tel: 0577-65690019    E-mail: ichenjian@gmail.com    MSN:ichenjian@hotmail.com    QQ:2911194
    Copyright © 2004-2006 wengu.org 文谷 All Rights Reserved
    浙ICP备05000327号