新建一个类库Res用来存储资源文件。如图:

2.为Res项目添加资源文件Res.resx
并添加三个资源:
HongYe.ico
girl.gif
字符串strTest值Hello World
3.把Res.resx访问修饰符由Internal修改为Public
这样可以在其他项目中调用。
4.为UseRes添加Res项目引用。
5.读取资源的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UseRes
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void btnRead_Click(object sender, EventArgs e)
{
//读取资源
Icon icon = Res.Res.HongYe;
Bitmap bitmap = Res.Res.girl;
string strTest = Res.Res.strTest;
//
this.Icon = icon;
this.pictureBox1.Image = bitmap;
MessageBox.Show(strTest);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UseRes
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void btnRead_Click(object sender, EventArgs e)
{
//读取资源
Icon icon = Res.Res.HongYe;
Bitmap bitmap = Res.Res.girl;
string strTest = Res.Res.strTest;
//
this.Icon = icon;
this.pictureBox1.Image = bitmap;
MessageBox.Show(strTest);
}
}
}
运行结果

本文源码:http://dotnet.5d6d.com/thread-318-1-1.html