实例讲解.NET中资源文件的创建与使用(转)

真正的JavaScript加密和网页资源加密系统,从原理到实现 网站作为网民获取信息的主要来源之一,基于Html5的建站服务和应用越来越多,而Html5存在一个关键问题: 网页内的资源(包括了javascript、文字、图像等)可以通过非常简单的方式获取! 目前网络上提供了各种针对javascript(简称js)的加密手段,主要还是以混淆,Base64,去逻辑化等手段进行保护。先不说此类的加密是否安全,但是针对内部链接、文字、图像相关的数据并没有合理的加密手段。这个一方面的原因是js需要浏览器的javascript引擎完成执行,直接密码后的js是无法运行的,于是就出 阅读详情

原文:http://blog.youkuaiyun.com/lizanhong/archive/2004/09/10/100812.aspx

 

 

实例讲解.NET中资源文件的创建与使用

 

一、资源文件

资源文件顾名思义就是存放资源的文件。资源文件在程序设计中有着自身独特的优势,他独立于源程序,这样资源文件就可以被多个程序使用。同时在程序设计的时候,有时出于安全或者其他方面因素的考虑,把重要东西存放在资源文件中,也可以达到保密、安全的效果。那么Visual C#所使用的资源文件中到底存放哪些东西呢?在用Visual C#创建资源文件大致可以存放三种类型的数据资源,分别是字节数组、各种对象和字符串。本文将结合一个程序例子来具体说明用Visual C#是如何创建资源文件的。

二、创建资源文件所用的类

在.Net FrameWork SDK中的一个名字叫System.Resources名称空间,在此名称空间中为应用程序提供了许多创建、存储和使用资源文件的类和接口。其中有一个类叫ResourceWriter,Visual C#就是通过调用这个类来实现创建、存储资源文件的。

三、创建资源文件的方法

首先要继承一个ResourceWriter类,然后调用ResourceWriter类的一个方法Generate ( ),就可以产生一个资源文件了。具体语句如下:

ResourceWriter rw = new ResourceWriter ( "My.resources" ) ;

rw.Generate ( ) ;

此时在磁盘的中就会产生一个名称为"My.resources"的资源文件,但此时的资源文件没有任何内容,下面我们就来看看如何往资源文件中添加资源。

 

四、往资源文件中添加资源的方法
      
在ResourceWriter类中提供了一个AddResource ( )方法,这个方法的作用就是往资源文件中添加资源的。在Visual C#中对不同的资源有着不同的加入方式。

(1).加入字节数组,语法格式为:

public void AddResource ( string , byte [ ] ) ;

注释:其中string是在使用资源文件的时候,此字节数组在程序中的的唯一标识符

(2).加入对象,语法格式为:

public void AddResource ( string , object );

注释:其中string是在使用资源文件的时候,此对象在程序中的唯一标识符。如:
Image image1 = Image.FromFile ("abc1.jpg") ;

Image image2 = Image.FromFile ( "abc2.jpg" ) ;                           

rw.AddResource ( "abc1" , image1 ) ;

rw.AddResource ( "abc2" , image2 ) ;

(3).加入字符串,具体语法如下:

public void AddResource ( string1 , string2) ;

注释:其中string1是在使用资源文件的时候,此字符串在程序中的唯一标识符在本文的程序中,是如此使用的:

rw.AddResource ( "MyStr" , "从资源文件中读取字符串!" );

至此我们已经创建了一个资源文件,并且在资源文件中加入了若干个资源,当然在这之后,还应该注意,保存此资源文件,并关闭资源文件,具体如下:

rw.Close ( ) ;

五、示例创建资源文件

       在这里创建一个什么样的工程好呢?有些朋友可能会用.net创建一个“控制台应用程序”,其实没必要,直接用记事本创建一个CS文件就可以了,假如名称为:CreatResources.cs。编译命令:csc CreatResources.cs;运行:CreatResources。这时会产生一个叫做My.resources的资源文件。先放到这里,等下再用。

using System ;

using System.Drawing ;

using System.Resources ;

 

class CreatResource

{

       public static void Main ( )

       {

              ResourceWriter rw = new ResourceWriter ( "My.resources" ) ;

             

              Image image1 = Image.FromFile ("abc1.jpg") ;

              Image image2 = Image.FromFile ( "abc2.jpg" ) ;

                           

              rw.AddResource ( "abc1" , image1 ) ;

              rw.AddResource ( "abc2" , image2 ) ;

             

              Icon ic = new Icon("CDDRIVE.ICO");

              rw.AddResource( "abc3",ic);

              rw.AddResource( "abc4","这是从资源文件中读出的字符");

             

              rw.Generate ( ) ;

              rw.Close ( ) ;

       }

}

 

六、将生成的资源文件添加测试工程中的方法
      
创建一个“Windows应用程序”测试工程,将My.resources资源文件添加到该工程中,步骤如下:

       1,在资源管理器里选中文件

2,按住鼠标左键,拖到工程文件上,松开鼠标左键。

3,在拖放的文件上点鼠标右键,选“属性”

4,在生成操作里选择“嵌入的资源”。

 

七、如何在程序中管理资源文件中的资源

       在.Net FrameWork SDK这提供了一个关于资源文件创建和使用的名称空间--System.Resources。在这个名称空间中有一个Class为ResourceManager,这个Class的主要作用就是管理并使用资源文件。Visual C#是通过这个类来管理并使用嵌入程序中的资源文件中的资源。下列代码就是定义一个ResourceManager类来管理嵌入程序资源文件中的资源:

ResourceManager rm = new ResourceManager ( " Res.My " , Assembly.GetExecutingAssembly ( ) ) ;

       注意:ResourceManager rm = new ResourceManager ( " Res.My " , Assembly.GetExecutingAssembly ( ) ) ;语句中,构造函数的第一个参数Res.My 由两部分构成,Res表示测试工程的命名空间,My表示资源文件名My.resources的根名称,即点号有前部分My。

 

八、如何在程序中使用资源文件中的资源

在上一篇文章中,我们已经了解到在创建资源文件的时候,使用了AddResource ( )方法来加入资源,他的语法中的第一个参数是用户可以定义的字符串,这个字符串就是资源在资源文件的唯一标识,在程序设计中,就是通过这个唯一标识符来使用某个资源的。那么如何在程序中通过这个标识符来得到所需资源?这就要使用到ResourceManager类中的GetObject()和GetString()方法。这二个方法作用是获得指定的资源。下面是这二个方法的语法:

object GetSting(String)

object GetObject(String)

其中的"String"就是资源在资源文件中的那个唯一标识符。细心的读者可能已经注意到,这二个方法的返回值都是一个Object类型的变量,也就是一个参考类型的变量,而在程序中的字符串或者图象等,是一个实值类型变量。这就需要进行转换,而这种转换就是上面所说的装箱和出箱。下列代码是从资源文件中提取字符串、图象和图标资源:

提取字符串资源:

String s = ( ( String ) rm.GetString ( "MyStr" ) ) ;

提取图标资源:

Icon icoDemo = ( ( Icon ) rm.GetObject ( "demo.ico" ) ) ;

提取图象资源:

Image a = ( ( Image ) ( rm.GetObject ( "ok-off.png" ) ) ) ;

 

九、测试工程源代码
using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

// 加入以下两个命名空间。

using System.Resources;

using System.Reflection;

 

namespace Res

{

    ///

    /// Form1 的摘要说明。

    ///

    public class Form1 : System.Windows.Forms.Form

    {

        private System.Windows.Forms.PictureBox pictureBox1;

        private System.Windows.Forms.Button button1;

        private System.Windows.Forms.Button button2;

        private System.Windows.Forms.Button button3;

        private System.Windows.Forms.Label label1;

        private System.Windows.Forms.Button button4;

        ///

        /// 必需的设计器变量。

        ///

        private System.ComponentModel.Container components = null;

 

        public Form1()

        {

            //

            // Windows 窗体设计器支持所必需的

            //

            InitializeComponent();

 

            //

            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码

            //

        }

 

        ///

        /// 清理所有正在使用的资源。

        ///

        protected override void Dispose( bool disposing )

        {

            if( disposing )

            {

                if (components != null)

                {

                    components.Dispose();

                }

            }

            base.Dispose( disposing );

        }

 

        #region Windows 窗体设计器生成的代码

        ///

        /// 设计器支持所需的方法 - 不要使用代码编辑器修改

        /// 此方法的内容。

        ///

        private void InitializeComponent()

        {

            this.pictureBox1 = new System.Windows.Forms.PictureBox();

            this.button1 = new System.Windows.Forms.Button();

            this.button2 = new System.Windows.Forms.Button();

            this.button3 = new System.Windows.Forms.Button();

            this.label1 = new System.Windows.Forms.Label();

            this.button4 = new System.Windows.Forms.Button();

            this.SuspendLayout();

            //

            // pictureBox1

            //

            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

            this.pictureBox1.Location = new System.Drawing.Point(8, 8);

            this.pictureBox1.Name = "pictureBox1";

            this.pictureBox1.Size = new System.Drawing.Size(256, 240);

            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

            this.pictureBox1.TabIndex = 0;

            this.pictureBox1.TabStop = false;

            //

            // button1

            //

            this.button1.Location = new System.Drawing.Point(280, 8);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(88, 24);

            this.button1.TabIndex = 1;

            this.button1.Text = "图像1";

            this.button1.Click += new System.EventHandler(this.button1_Click);

            //

            // button2

            //

            this.button2.Location = new System.Drawing.Point(280, 48);

            this.button2.Name = "button2";

            this.button2.Size = new System.Drawing.Size(88, 24);

            this.button2.TabIndex = 2;

            this.button2.Text = "图像2";

            this.button2.Click += new System.EventHandler(this.button2_Click);

            //

            // button3

            //

            this.button3.Location = new System.Drawing.Point(280, 128);

            this.button3.Name = "button3";

            this.button3.Size = new System.Drawing.Size(88, 24);

            this.button3.TabIndex = 2;

            this.button3.Text = "文字";

            this.button3.Click += new System.EventHandler(this.button3_Click);

            //

            // label1

            //

            this.label1.Location = new System.Drawing.Point(8, 264);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(360, 16);

            this.label1.TabIndex = 4;

            this.label1.Text = "label1";

            //

            // button4

            //

            this.button4.Location = new System.Drawing.Point(280, 88);

            this.button4.Name = "button4";

            this.button4.Size = new System.Drawing.Size(88, 24);

            this.button4.TabIndex = 2;

            this.button4.Text = "图标";

            this.button4.Click += new System.EventHandler(this.button4_Click);

            //

            // Form1

            //

            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

            this.ClientSize = new System.Drawing.Size(376, 285);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.button2);

            this.Controls.Add(this.button1);

            this.Controls.Add(this.pictureBox1);

            this.Controls.Add(this.button3);

            this.Controls.Add(this.button4);

            this.Name = "Form1";

            this.Text = "Form1";

            this.ResumeLayout(false);

 

        }

        #endregion

 

        ///

        /// 应用程序的主入口点。

        ///

        [STAThread]

        static void Main()

        {

            Application.Run(new Form1());

        }

 

        private void button1_Click(object sender, System.EventArgs e)

        {

            System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly());

           

            this.pictureBox1.Image = (Bitmap)rm.GetObject("abc1");

        }

 

        private void button2_Click(object sender, System.EventArgs e)

        {

            System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly());

            this.pictureBox1.Image = (Bitmap)rm.GetObject("abc2");

        }

 

        private void button4_Click(object sender, System.EventArgs e)

        {

            System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly());

            this.Icon = (Icon)rm.GetObject("abc3");

        }

 

        private void button3_Click(object sender, System.EventArgs e)

        {

            System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly());

            this.label1.Text = rm.GetString("abc4");

        }

    }

}

 

十、结果图

 

十一、源代码下载网址
http://lizanhong.myshow.cn/

 

十二、感谢

       这篇文章的大部分文字版权属于王天所有,本来这篇文章没必要写的,因为王天网友已经写得很清楚了,但是我根据王天的文章操作一遍之后发现有些问题没说清楚,所以我重新整理了一下。

 

十三、参考

http://www8.ccidnet.com/tech/guide/2002/01/22/92_3940.html

http://www.socent.com/dotnet/message.asp?id=218

.net资源文件生成器 NET资源文件生成器(ResourceWorkshop)是专业的资源文件开发工具,集成了VS.NET中的所有资源文件开发工具的功能,是.NET程序员的必备工具。它有中英文两种语言版本,支持多文档窗口,具有独特的文件关联功能,使您编缉资源文件时更加得心应手。 主要功能: 1、支持.NET框架下的三种资源文件格式(.txt文件、.resX文件和.Resources文件)。 2、支持字符串、颜色、图片、光标等各种资源。 3、提供了添加、删除、重命名、剪切、复制、粘贴等常用编缉功能。 4、支持多文档窗口,可同时编缉多个文件。 5、可将不同资源文件格式之间的相互转换。 6、可将多个不同格式的资源文件合并为一个资源文件。 7、提供了资源文件关联功能,便于对照比较多个资源文件。 设置资源文件关联,是本程序的一个特色功能。将多个资源文件关联在一起,并将其中一个作为主动文件,当主动文件的当前资源项改变时,其它文件中的当前资源项也随之变化,指向与之同名的资源。类似Foxbase中数据库之间的关联功能,被动数据库中的记录指针随着主动数据库的记录指针变化。 当被动资源文件中找不到与主动资源文件中同名的资源时,根据不同的设置,可定位到第一项资源或询问是否复制资源或自动复制资源。 请到我们的主页http://www.guohuasoft.com下载最新版本。 立即下载

相关推荐

VB.NET调用资源文件图片

调用资源文件 添加一幅图片资源到一个外部的资源文件中,这个图片将显示在一个pictureBox中,其实直接在设计时设置好控件的Image就可以了

实例讲解.NET中资源文件的创建与使用

实例讲解.NET中资源文件的创建与使用 一、资源文件资源文件顾名思义就是存放资源的文件。资源文件在程序设计中有着自身独特的优势,他独立于源程序,这样资源文件就可以被多个程序使用。同时在程序设计的时候,有时出于安全或者其他方面因素的考虑,把重要东西存放在资源文件中,也可以达到保密、安全的效果。那么Visual C#所使用的资源文件中到底存放哪些东西呢?在用Visual C#创建资源文件大

ddarkelf的专栏 1014

.NET 资源文件resx、Resources详细说明

资源文件简介 (1)resx文件: 基于文本的格式是特定于.NET 框架的 XML 格式,称为 ResX(.resx 文件)。不考虑其 XML 基础,该格式不是专门为人工阅读而设计的(XML 格式很少是这样的)。但是,Visual Studio .NET 仍然为 .resx 文件提供了一个基本编辑器。 (2)Resources文件: .resources 扩展名来自于在将 .resx 文件作为资源嵌入之前 Visual Studio .NET 处理该文件时所使用的工具。工具名称是 resgen.exe,它用来将 .resx XML 格式“编译”为二进制格式。可以手动将 .resx 文件编译成

asp.net 资源文件的使用

通过简单的设置和引用即可实现不同语言的转变 1、新建资源文件Resource.resx,VS2005自动将其放在App_GlobalResources文件夹中; 2、打开资源文件,输入常用的字符串,文件里面是一些key/value的集合,key用来做引用的标识,value用来表示字符串的值;(保存后可以看到Resource.resx实际是一个xml文件) 3、在系统里面进行引用: ...

191

在.NET中读取嵌入和使用资源文件的方法

Dotnet中嵌入资源(位图、图标或光标等)有两种方式,一是直接把资源文件加入到项目,作为嵌入资源,在代码中通过Assembly的GetManifestResourceStream方法获取资源的Stream。另一种方法是在项目中加入. resx资源文件,在资源文件中添加资源,由ResourceManager类统一管理其中的资源。 下面分别详述这两种方法 一、使用GetManifestResourceStream读取嵌入资源 1、加入资源文件 直接把要嵌入到程序集的资源文件加入到项目中,可以加在项目的根目录,可以加在项目的任何目录中。 2、设置资源文件的“BuildAction”属性 将嵌入

异常数据处理——3σ原则、箱线图

异常数据处理——3σ原则、箱线图 用不包括异常值的剩余数据的均值替换、删除DataFrame中某列有异常值的整行等方法

杨老师的小跟班(*∩_∩*) 9033

关于.NET使用资源文件

 .NET使用资源文件1、首先新建工程2、添加资源文件,InfoDesciption.resx3、添加信息到资源文件,如图所示4、读取资源文件信息下面分别介绍asp.net 和 windows应用程序读取资讯文件的方法:asp.net 在.NET2.0中读取资源文件很简单code: using System.Reflection;using Syst

ABC123 1717

与rkipc通信

函数名称功能系统模块初始化系统模块反初始化重启恢复出厂设置导出日志导出配置文件导入配置文件系统升级获取用户数设置用户数获取用户权限设置用户权限获取用户名设置用户名获取密码(已加密)设置密码(已加密)新增用户删除用户。

qq_44643189的博客 1611

.NET Core(C#)使用AngleSharp下载网页源码及资源文件(html,css,js,jpg等图片)

本文介绍.NET Core(C#)中,使用AngleSharp下载网页的html代码,及网页引用的(JS)javascript,css样式表文件,和图片(jpg,png等)的下载的方法,以及相关的示例代码。 原文地址:.NET Core(C#)使用AngleSharp下载网页源码及资源文件(html,css,js,jpg等图片) ...

weixin_42098295的博客 638

别再找在线API了!用C#调用微信电脑版自带的OCR,本地识别又快又准

本文详细介绍了如何利用C#调用微信电脑版内置的OCR引擎实现本地文字识别,解决传统云端API的隐私和网络依赖问题。微信OCR具备高精度中文识别、完全离线运行和零成本接入三大优势,特别适合企业文档电子化处理。通过实战代码演示了核心交互逻辑、批量处理流水线和表格数据提取等高级应用场景。

weixin_29295541的博客 588

【RS】哨兵系列数据下载(哨兵2号Sentinel-2波段介绍、下载)

以哨兵二号为示例,分享如何下载欧空局的哨兵系列数据

Remote Sensing迷途小书童 1万+

.NET(C#) 读取Resource资源文件的方法

System.Resources 名字空间支持三种资源文件:.txt 文件,只能有字符串资源。.resources 文件,PE 格式,可以加入任何资源。唯一可以被嵌入到 Assembly 的文件,在 System.Resources 名字空间中有专用读写的类。本文主要介绍.NET(C#) 读取Resource资源文件的方法,以及相关的示例代码。直接添加文件到.Resx资源文件中,在运行时使用Resource Manager读取,代码如下,在VS中将文件的生成操作设置“嵌入的资源”的读取方法,代码如下。

weixin_53370274的博客 1万+

电影台词预测类型:NLP小样本多标签分类实战

电影类型预测本质上是短文本语义建模问题,核心在于从高度碎片化、低语义密度的台词(平均仅12.7词)中挖掘隐含的类型学编码规则。其技术难点在于:需应对省略主语、倒装、拟声词等非规范表达;处理Drama与Comedy等类型强共现的多标签耦合;并克服领域迁移障碍——维基文本预训练模型在台词上性能骤降0.11。解决方案聚焦RoBERTa微调、类型感知注意力机制与标签平滑策略,兼顾小样本鲁棒性与业务可解释性,广泛适用于影视元数据标注、智能推荐系统及NLP教学实践。

weixin_33309048的博客 288

网页unescape码加密与解密

 

山炮之家 1621

2024年数字IC秋招-华为机试-数字芯片-实习笔试题

笔试题目:40个选择题A.综合时不同文件列表内的宏定义仅在本列表生效B.宏定义会覆盖,不需要单独undefC.仿真时出现宏定义冲突以最后一次值为准D.宏定义最好在各文件中随用随定义A.4/9B.4/6C.01D.0/6。

qq_42135020的博客 2892

OSPF的7种状态,DR与BDR的基本概念,1、2、3、4、5、7类LSA解析,Stub、NSSA、Totally Stub、Totally NSSA的基本概念与配置

OSPF的7种状态,DR与BDR的基本概念,1、2、3、4、5、7类LSA解析,Stub、NSSA、Totally Stub、Totally NSSA的基本概念与配置

weixin_72194028的博客 3008

杰林码网页资源加密技术在网络小说方面的应用

杰林码网页资源加密技术在网络小说方面的应用,主要可以体现在以下几个方面:

王杰林(编码技术)的博客 821

Cadence OrCAD Capture CIS元器件管理时ERROR(ORCIS-6454): Temporary part number tracking is turned on报错的解决方法

使用Part manager对工程的元器件进行批量管理时,出现如下图所示的ERROR(ORCIS-6454): Temporary part number tracking is turned on报错其解决方法如下。

fydar的博客 3501

网页的加密解密之8位二进制ASCII码加密解密方法(转载)

网页的加密解密之8位二进制ASCII码加密解密方法(转载) 作者:轩辕小聪转载于:卡卡论坛 提示:接触恶意网页有风险,如果直接用IE浏览器打开不经修改的带有恶意脚本的网页,又没有打好相应补丁的话,就等于直接把病毒“引进家门”。除非你真正有必要做这些甚至已经把它当成了“家常便饭”,否则绝不提倡去尝试。原恶意网页地址:http://www.bartenderofchina.com/

飘的梦客厅 4672

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

linux网页加密码,Linux 文件加密

1. base64编码/解码谈到命令行下怎样发送邮件附件,很多人想起了uuencode。也能够使用base64编码。以下是openssl base64编码/解码的使用:$ openssl base64 filename.txt$ openssl base64 -d filename.bin2. 校验文档的一致性UNIX下校验文档一致性的方法很多,比如sum、cksum、md5sum、sha...

weixin_30249953的博客 300

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

在dotnet中如何使用资源(转自MS中国社区)

如何使用资源文件 摘要 .NET 中有一套非常完善的地方化系统被定义在 System.Resources 名字空间中。不过大多数人都被 MissingManifestResourceException 这个错误困惑着。本文就是要让大家了解什么是资源文件,它有什么用处以及如何正确的调用从而避免一些"奇怪"的错误,包括 MissingManifestResourceException 这个常见错误

Sharper的技术人生 1441

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

在.NET中嵌入和使用资源文件

嵌入和使用资源文件,以下是全部源代码:////= null)//// pBox////////////// txtInfo////// Form1//

biyusr的专栏 691

.net中使用资源文件

.net 支持三种文件类型的resource:.txt  .resx  .resources system.resources 名字空间支持三种资源文件: .txt 文件,只能有字符串资源。因为不能被嵌入到 assembly 中,所以非常容易暴露,被客户修改。最大缺点是仅支持字符串资源,所以不推荐使用。 .resx 文件,由 xml 组成,能加入所有资源,包括二进制。...

weixin_34357962的博客 164

.net中资源文件的使用

  .Net是一个丰富的平台,在它的结构中,程序集Assembly是自我描述的安装单元,它可以只包括一个PE(可移植可执行)格式的Dll或exe文件,也可以由多个文件组成,例如资源、Dll和exe等。 其中,资源是的范围很广,它可由多种元素组成,包括与用户交互的界面元素(如位图、图标或光标)、应用程序所需数据的自定义文件以及安装 API 使用的版本文件、菜单和对话框等都可以作为资源。为.Net程序...

weixin_30550271的博客 179
上一篇: 捕捉DataGrid的双击事件(C#版本)
下一篇: C#实现动态灵活调用业务方法的机制
colderboy
博客等级 码龄26年 1粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值