基本信息
源码名称:C# 读取资源文件的三种方法 示例源码下载
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2016-11-02
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

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;
using System.IO;
using System.Resources;
using System.Collections;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ////string temp_Namespace = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace;  // 獲取命名空間名稱
            ////pictureBox1.BackgroundImage = global::工程名.Properties.Resources.图片名(没有后缀);

            // 方法1

            string s = global::WindowsFormsApplication4.Properties.Resources.Key;

            textBox1.Text = s   Environment.NewLine;

            // 方法2

            // 图片调用:Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"TestCustomForm.Res.button.btndown.bmp"));
            //其中:TestCustomForm为项目名称,Res为项目下的文件夹,button为Res的子文件夹,btndown.bmp是文件名称。

            System.IO.StreamReader sr = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(@"WindowsFormsApplication4.Resources.Key.txt"));

            byte[] bytes = sr.CurrentEncoding.GetBytes(sr.ReadToEnd());

            string ss = Encoding.Default.GetString(bytes);

            textBox1.Text  = ss   Environment.NewLine;

            //方法3

            string sss = this.getStringResource("Resources.Key.txt");

            textBox1.Text  = sss   Environment.NewLine;



            ////////System.Resources.ResourceManager manager =new System.Resources.ResourceManager("资源名称", Assembly.GetExecutingAssembly());

            ////////Object target = manager.GetObject("资源文件名");//获取到指定的资源的值

            //////////在这里对应我们加入到资源文件的清单,

            //////////转换回我没的目标值,可能会产生一个拆箱的过程

            //////////如果我们资源文件对应的该清单是一个图片文件System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)target;

            //////////如果我们的资源文件是光标文件

            ////////Cursor cursor = (Cursor)target;

            //////////图标

            ////////Icon icon = (Icon)target;

            //////////字符串

            ////////string str = target.ToString();//或者manager

            ////////manager.GetString("资源文件名");


        }



        private string getStringResource(String name)
        {

            //string temp_Namespace = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace;  // 獲取命名空間名稱

            System.Reflection.Assembly asm = this.GetType().Assembly;

            //在这个文件中列出的所有资源

            //foreach (String rs in asm.GetManifestResourceNames())

            //System.Diagnostics.Debug.WriteLine(rs);

            name = this.GetType().Namespace   "."   name;

            System.IO.Stream strm = asm.GetManifestResourceStream(name);  // "WindowsFormsApplication4.WindowsFormsApplication4.Resources.Key.txt"	string

            //默认的系统编码转换为字符串

            return new System.IO.StreamReader(strm, System.Text.Encoding.Default).ReadToEnd();

        }




    }
}