C#资源文件操作示例 创建资源和读取资源

1.新建一个Windows应用程序UseRes,用来调用资源。
新建一个类库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);
        }
    }
}
原文:http://www.it100.info/res.html
运行结果

本文源码:http://dotnet.5d6d.com/thread-318-1-1.html  
天祺围棋:www.tianqiweiqi.com呵呵

凡事以大气象去面对,优秀是一种习惯。

原文地址:https://www.cnblogs.com/greatverve/p/res.html