DotNet编程-星光伴我行

滴滴真谛 水滴石穿

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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 窗体设计器生成的代码

        
/// <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

        
/// <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();
            }

        }

    }

}

posted on 2006-04-30 18:13  DotNet编程  阅读(964)  评论(0编辑  收藏  举报