zoukankan
html css js c++ java
读写资源文件
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
using
System.Resources;
using
ResEditorComponents ;
namespace
读写资源文件
{
/**/
///
<summary>
///
Form1 的摘要说明。
///
</summary>
public
class
Form1 : System.Windows.Forms.Form
{
private
System.Windows.Forms.Button button1;
private
System.Windows.Forms.Button button2;
private
System.Windows.Forms.Button button3;
private
System.Windows.Forms.Button button4;
private
System.Windows.Forms.Button button5;
private
System.Windows.Forms.PictureBox pictureBox1;
/**/
///
<summary>
///
必需的设计器变量。
///
</summary>
private
System.ComponentModel.Container components
=
null
;
public
Form1()
{
//
//
Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
//
TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**/
///
<summary>
///
清理所有正在使用的资源。
///
</summary>
protected
override
void
Dispose(
bool
disposing )
{
if
( disposing )
{
if
(components
!=
null
)
{
components.Dispose();
}
}
base
.Dispose( disposing );
}
Windows 窗体设计器生成的代码
#region
Windows 窗体设计器生成的代码
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.button1
=
new
System.Windows.Forms.Button();
this
.button2
=
new
System.Windows.Forms.Button();
this
.button3
=
new
System.Windows.Forms.Button();
this
.button4
=
new
System.Windows.Forms.Button();
this
.button5
=
new
System.Windows.Forms.Button();
this
.pictureBox1
=
new
System.Windows.Forms.PictureBox();
this
.SuspendLayout();
//
//
button1
//
this
.button1.Location
=
new
System.Drawing.Point(
296
,
48
);
this
.button1.Name
=
"
button1
"
;
this
.button1.Size
=
new
System.Drawing.Size(
144
,
23
);
this
.button1.TabIndex
=
0
;
this
.button1.Text
=
"
写资源文件(字符串)
"
;
this
.button1.Click
+=
new
System.EventHandler(
this
.button1_Click);
//
//
button2
//
this
.button2.Location
=
new
System.Drawing.Point(
296
,
88
);
this
.button2.Name
=
"
button2
"
;
this
.button2.Size
=
new
System.Drawing.Size(
144
,
23
);
this
.button2.TabIndex
=
0
;
this
.button2.Text
=
"
写资源文件(图像)
"
;
this
.button2.Click
+=
new
System.EventHandler(
this
.button2_Click);
//
//
button3
//
this
.button3.Location
=
new
System.Drawing.Point(
224
,
128
);
this
.button3.Name
=
"
button3
"
;
this
.button3.Size
=
new
System.Drawing.Size(
208
,
23
);
this
.button3.TabIndex
=
1
;
this
.button3.Text
=
"
系统所带的资源文件操作源码
"
;
this
.button3.Click
+=
new
System.EventHandler(
this
.button3_Click);
//
//
button4
//
this
.button4.Location
=
new
System.Drawing.Point(
296
,
168
);
this
.button4.Name
=
"
button4
"
;
this
.button4.TabIndex
=
2
;
this
.button4.Text
=
"
button4
"
;
//
//
button5
//
this
.button5.Location
=
new
System.Drawing.Point(
256
,
208
);
this
.button5.Name
=
"
button5
"
;
this
.button5.Size
=
new
System.Drawing.Size(
192
,
32
);
this
.button5.TabIndex
=
3
;
this
.button5.Text
=
"
button5
"
;
this
.button5.Click
+=
new
System.EventHandler(
this
.button5_Click);
//
//
pictureBox1
//
this
.pictureBox1.Location
=
new
System.Drawing.Point(
16
,
24
);
this
.pictureBox1.Name
=
"
pictureBox1
"
;
this
.pictureBox1.Size
=
new
System.Drawing.Size(
200
,
176
);
this
.pictureBox1.TabIndex
=
4
;
this
.pictureBox1.TabStop
=
false
;
//
//
Form1
//
this
.AutoScaleBaseSize
=
new
System.Drawing.Size(
6
,
14
);
this
.ClientSize
=
new
System.Drawing.Size(
520
,
266
);
this
.Controls.Add(
this
.pictureBox1);
this
.Controls.Add(
this
.button5);
this
.Controls.Add(
this
.button4);
this
.Controls.Add(
this
.button3);
this
.Controls.Add(
this
.button1);
this
.Controls.Add(
this
.button2);
this
.Name
=
"
Form1
"
;
this
.Text
=
"
Form1
"
;
this
.Load
+=
new
System.EventHandler(
this
.Form1_Load);
this
.ResumeLayout(
false
);
}
#endregion
/**/
///
<summary>
///
应用程序的主入口点。
///
</summary>
[STAThread]
static
void
Main()
{
Application.Run(
new
Form1());
}
//
将字符串写入资源文件
private
void
button1_Click(
object
sender, System.EventArgs e)
{
//
Creates a resource writer.
IResourceWriter writer
=
new
ResourceWriter(
"
myString.resources
"
);
//
Adds resources to the resource writer.
writer.AddResource(
"
ID
"
,
"
0001
"
);
writer.AddResource(
"
NAME
"
,
"
FURENJUN
"
);
writer.AddResource(
"
AGE
"
,
"
22
"
);
//
Writes the resources to the file or stream, and closes it.
writer.Close();
MessageBox.Show(
"
success
"
) ;
}
private
void
Form1_Load(
object
sender, System.EventArgs e)
{
}
private
void
button2_Click(
object
sender, System.EventArgs e)
{
//
Creates a resource writer.
ResourceWriter rw
=
new
ResourceWriter (Application.StartupPath
+
@"
\picture.resources
"
);
//
从指定的文件创建Image对象.
OpenFileDialog openFileDialog1
=
new
OpenFileDialog();
openFileDialog1.InitialDirectory
=
"
c:\\
"
;
openFileDialog1.Filter
=
"
图像文件 (*.jpg)|*.jpg|All files (*.*)|*.*
"
;
openFileDialog1.FilterIndex
=
1
;
openFileDialog1.RestoreDirectory
=
true
;
openFileDialog1.Multiselect
=
true
;
if
(openFileDialog1.ShowDialog()
==
DialogResult.OK)
{
string
[] fileName1
=
openFileDialog1.FileNames;
int
i
=
fileName1.Length ;
for
(
int
j
=
0
;j
<
i;j
++
)
{
//
把Image对象添加到资源文件中
//
ResourceWritername.AddResource(string name, object value);
//
其中name为资源名,value为资源值
string
ImageName
=
"
Img
"
+
j.ToString();
Image Img
=
Image.FromFile (fileName1[j]);
rw.AddResource ( ImageName , Img );
}
}
//
Writes the resources to the file or stream, and closes it.
rw.Generate ();
rw.Close ();
MessageBox.Show(
"
success
"
) ;
}
private
void
button3_Click(
object
sender, System.EventArgs e)
{
ResEditor redit
=
new
ResEditor();
redit.Show();
}
private
void
button5_Click(
object
sender, System.EventArgs e)
{
//
初始化 ResourceManager 类的新实例,它查找包含在一些文件中的资源,
//
这些文件是使用给定的 Assembly 从指定根名称导出的。
//
格式
//
public ResourceManager(
//
string baseName,
//
Assembly assembly
//
);
//
读取资源文件
ResourceManager rm;
//
rm = new ResourceManager("读写资源文件.picture", this.GetType().Assembly);
//
应用(2.1)写法
rm
=
new
ResourceManager(
"
读写资源文件.image.picture
"
,
this
.GetType().Assembly);
//
应用(2.2)写法
this
.pictureBox1.Image
=
(System.Drawing.Image)rm.GetObject(
"
Img0
"
);
rm
=
new
ResourceManager(
"
读写资源文件.myString
"
,
this
.GetType().Assembly);
MessageBox.Show(rm.GetObject(
"
ID
"
).ToString() ) ;
//
备注:
//
1.一定要将生成的资源文件添加到当前项目中,并将其设为"嵌入的资源".
//
2.如果你资源文件在debug文件夹下,
//
(2.1) (资源的根名称)baseName格式为:<default namespace.><filename>
//
如果资源的文件在该项目的子文件夹中,
//
(2.2) baseName格式为:<default namespace.><child folder name.><filename>
}
//
系统所带资源文件编辑工具源代码
//
D:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Samples\Tutorials\resourcesandlocalization\reseditor
//
Environment.GetCommandLineArgs()
//
只有 .resources 文件才应嵌入在公共语言运行库程序集和附属程序集中。
//
资源文件生成器 (Resgen.exe) 将文本 (.txt) 文件和基于 XML 的资源 (.resx) 文件转换成
//
.resources 文件,方法是包装由 ResourceReader、ResourceWriter、ResXResourceReader
//
和 ResXResourceWriter 类实现的方法。您还可以使用 Resgen.exe 来将 .resources 文件
//
转换回 .resx 和 .txt 文件。
//
D:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Samples\Tutorials\resourcesandlocalization\resxgen
}
}
//
读取.exe文件中的图片,并将其保存下来
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
using
System.Reflection;
using
System.IO;
namespace
EmbeddedResources1
{
/**/
///
<summary>
///
Summary description for Form1.
///
</summary>
public
class
MainForm : System.Windows.Forms.Form
{
private
Assembly loadedAssembly
=
null
;
private
System.Windows.Forms.Panel panel1;
private
System.Windows.Forms.Button loadAssembly;
private
System.Windows.Forms.ListBox resources;
private
System.Windows.Forms.Panel panel2;
private
System.Windows.Forms.PictureBox image;
private
System.Windows.Forms.Splitter splitter;
private
System.Windows.Forms.Button save;
private
System.Windows.Forms.OpenFileDialog ofd;
private
System.Windows.Forms.SaveFileDialog sfd;
/**/
///
<summary>
///
Required designer variable.
///
</summary>
private
System.ComponentModel.Container components
=
null
;
public
MainForm()
{
//
//
Required for Windows Form Designer support
//
InitializeComponent();
//
//
TODO: Add any constructor code after InitializeComponent call
//
}
/**/
///
<summary>
///
Clean up any resources being used.
///
</summary>
protected
override
void
Dispose(
bool
disposing )
{
if
( disposing )
{
if
(components
!=
null
)
{
components.Dispose();
}
}
base
.Dispose( disposing );
}
Windows Form Designer generated code
#region
Windows Form Designer generated code
/**/
///
<summary>
///
Required method for Designer support - do not modify
///
the contents of this method with the code editor.
///
</summary>
private
void
InitializeComponent()
{
this
.panel1
=
new
System.Windows.Forms.Panel();
this
.save
=
new
System.Windows.Forms.Button();
this
.loadAssembly
=
new
System.Windows.Forms.Button();
this
.resources
=
new
System.Windows.Forms.ListBox();
this
.splitter
=
new
System.Windows.Forms.Splitter();
this
.panel2
=
new
System.Windows.Forms.Panel();
this
.image
=
new
System.Windows.Forms.PictureBox();
this
.ofd
=
new
System.Windows.Forms.OpenFileDialog();
this
.sfd
=
new
System.Windows.Forms.SaveFileDialog();
this
.panel1.SuspendLayout();
this
.panel2.SuspendLayout();
this
.SuspendLayout();
//
//
panel1
//
this
.panel1.Controls.AddRange(
new
System.Windows.Forms.Control[]
{
this
.save,
this
.loadAssembly}
);
this
.panel1.Dock
=
System.Windows.Forms.DockStyle.Top;
this
.panel1.Name
=
"
panel1
"
;
this
.panel1.Size
=
new
System.Drawing.Size(
472
,
40
);
this
.panel1.TabIndex
=
3
;
//
//
save
//
this
.save.Location
=
new
System.Drawing.Point(
120
,
8
);
this
.save.Name
=
"
save
"
;
this
.save.Size
=
new
System.Drawing.Size(
80
,
23
);
this
.save.TabIndex
=
2
;
this
.save.Text
=
"
Save to file
"
;
this
.save.Click
+=
new
System.EventHandler(
this
.save_Click);
//
//
loadAssembly
//
this
.loadAssembly.Location
=
new
System.Drawing.Point(
8
,
8
);
this
.loadAssembly.Name
=
"
loadAssembly
"
;
this
.loadAssembly.Size
=
new
System.Drawing.Size(
104
,
23
);
this
.loadAssembly.TabIndex
=
0
;
this
.loadAssembly.Text
=
"
Load Assembly
"
;
this
.loadAssembly.Click
+=
new
System.EventHandler(
this
.loadAssembly_Click);
//
//
resources
//
this
.resources.Dock
=
System.Windows.Forms.DockStyle.Left;
this
.resources.IntegralHeight
=
false
;
this
.resources.Location
=
new
System.Drawing.Point(
0
,
40
);
this
.resources.Name
=
"
resources
"
;
this
.resources.Size
=
new
System.Drawing.Size(
128
,
323
);
this
.resources.Sorted
=
true
;
this
.resources.TabIndex
=
4
;
this
.resources.SelectedIndexChanged
+=
new
System.EventHandler(
this
.resources_SelectedIndexChanged);
//
//
splitter
//
this
.splitter.BackColor
=
System.Drawing.SystemColors.ControlDark;
this
.splitter.Location
=
new
System.Drawing.Point(
128
,
40
);
this
.splitter.MinExtra
=
205
;
this
.splitter.MinSize
=
128
;
this
.splitter.Name
=
"
splitter
"
;
this
.splitter.Size
=
new
System.Drawing.Size(
3
,
323
);
this
.splitter.TabIndex
=
5
;
this
.splitter.TabStop
=
false
;
//
//
panel2
//
this
.panel2.Controls.AddRange(
new
System.Windows.Forms.Control[]
{
this
.image}
);
this
.panel2.Dock
=
System.Windows.Forms.DockStyle.Fill;
this
.panel2.Location
=
new
System.Drawing.Point(
131
,
40
);
this
.panel2.Name
=
"
panel2
"
;
this
.panel2.Size
=
new
System.Drawing.Size(
341
,
323
);
this
.panel2.TabIndex
=
6
;
//
//
image
//
this
.image.Dock
=
System.Windows.Forms.DockStyle.Fill;
this
.image.Name
=
"
image
"
;
this
.image.Size
=
new
System.Drawing.Size(
341
,
323
);
this
.image.TabIndex
=
1
;
this
.image.TabStop
=
false
;
//
//
ofd
//
this
.ofd.DefaultExt
=
"
dll
"
;
this
.ofd.Filter
=
"
.NET Assemblies (*.dll;*.exe)|*.dll;*.exe|All Files (*.*)|*.*
"
;
this
.ofd.Title
=
"
Load an assembly
"
;
//
//
sfd
//
this
.sfd.Filter
=
"
All Files (*.*)|*.*
"
;
this
.sfd.Title
=
"
Save Resource As
"
;
//
//
MainForm
//
this
.AutoScaleBaseSize
=
new
System.Drawing.Size(
5
,
13
);
this
.ClientSize
=
new
System.Drawing.Size(
472
,
363
);
this
.Controls.AddRange(
new
System.Windows.Forms.Control[]
{
this
.panel2,
this
.splitter,
this
.resources,
this
.panel1}
);
this
.MinimumSize
=
new
System.Drawing.Size(
320
,
240
);
this
.Name
=
"
MainForm
"
;
this
.SizeGripStyle
=
System.Windows.Forms.SizeGripStyle.Show;
this
.Text
=
"
Embedded Resource Viewer
"
;
this
.panel1.ResumeLayout(
false
);
this
.panel2.ResumeLayout(
false
);
this
.ResumeLayout(
false
);
}
#endregion
/**/
///
<summary>
///
The main entry point for the application.
///
</summary>
[STAThread]
static
void
Main()
{
Application.Run(
new
MainForm());
}
private
void
loadAssembly_Click(
object
sender, System.EventArgs e)
{
ofd.FileName
=
""
;
if
( ofd.ShowDialog()
==
DialogResult.Cancel )
return
;
try
{
loadedAssembly
=
Assembly.LoadFrom(ofd.FileName);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
return
;
}
LoadResources();
}
private
void
LoadResources()
{
string
[] resourceNames
=
loadedAssembly.GetManifestResourceNames();
resources.Items.Clear();
if
( resourceNames.Length
>
0
)
{
resources.BeginUpdate();
foreach
(
string
resourceName
in
resourceNames)
{
resources.Items.Add(resourceName);
}
resources.EndUpdate();
}
Image img
=
image.Image;
image.Image
=
null
;
if
( img
!=
null
)
{
img.Dispose();
img
=
null
;
}
}
private
void
save_Click(
object
sender, System.EventArgs e)
{
if
( resources.SelectedIndex
<
0
)
return
;
sfd.FileName
=
(
string
) resources.SelectedItem;
if
( sfd.ShowDialog()
==
DialogResult.Cancel )
return
;
Stream outFile
=
sfd.OpenFile();
Stream inFile
=
loadedAssembly.GetManifestResourceStream((
string
) resources.SelectedItem);
long
length
=
inFile.Length;
if
( length
>
int
.MaxValue )
{
MessageBox.Show(
"
Unable to write file in this version, sorry
"
);
outFile.Close();
inFile.Close();
}
byte
[] bytes
=
new
byte
[length];
inFile.Read( bytes,
0
, (
int
) length );
outFile.Write( bytes,
0
, (
int
) length );
inFile.Close();
outFile.Close();
}
private
void
resources_SelectedIndexChanged(
object
sender, System.EventArgs e)
{
if
( resources.SelectedIndex
<
0
)
return
;
Stream stream
=
null
;
try
{
stream
=
loadedAssembly.GetManifestResourceStream((
string
) resources.SelectedItem);
Image img
=
Image.FromStream(stream);
Image oldImage
=
image.Image;
image.Image
=
img;
if
( oldImage
!=
null
)
{
oldImage.Dispose();
oldImage
=
null
;
}
}
catch
{
}
finally
{
if
( stream
!=
null
)
stream.Close();
}
}
}
}
查看全文
相关阅读:
[JAVA]Java下XML的解析
[短彩信]C#短彩信模块开发设计(4)——其他
[短彩信]C#短彩信模块开发设计(3)——协议
入住博客园了~
【Oyster轻量框架】C# ORM 较灵活的轻量数据模型
C# Oracle 之Insert 与 Update 的封装
用WORD发布博客测试~
C#自带的.net类库获得主机名和IP
百万本书籍视频资料免费送
教你使用ECode破解E语言crackme视频教程
原文地址:https://www.cnblogs.com/furenjun/p/389631.html
最新文章
通用权限管理设计 之 数据权限
jQuery LigerUI V1.1.6 (包括API和全部源码) 发布
jQuery LigerUI 使用教程入门篇
jQuery LigerUI 使用教程表格篇(3) 复选框、多表头、分组、汇总和明细
jQuery LigerUI 表格LigerGrid 结合 ASP.NET MVC 显示数据
分享一个基于ligerui的系统应用案例ligerRM V2(权限管理系统)(提供下载)
ligerui 提交反馈
各安卓版本关于loadLibrary函数的实现区别
利用shell脚本实现计划任务功能
利用shell脚本实现计划任务功能 V1.2
热门文章
linux bash script简明手册(持续更新)
NFS服务基本配置及使用
[短彩信]C#短彩信模块开发设计(2)——配置
[Tools]在Eclipse中外挂使用BeyondCompare CodeReview神器
[JAVA]使用Jersey处理Http协议中的Multipart
[c#]BPlusTreePrototype B+树的C#实现
[Java][转]Memcache —— 简单介绍:背景、原理、应用
[winform]帮邻居写的一个小软件沙盘治疗管理系统
[WPF]MVVM Demo
[JAVA]用Axis发布/调用Webservice
Copyright © 2011-2022 走看看