C#如何读取嵌入式资源并以字符串形式返回 [英] C# How to read embedded resource and return as string

查看:276
本文介绍了C#如何读取嵌入式资源并以字符串形式返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Streamreader读取嵌入式资源(txt文件)并将其作为字符串返回?我当前使用Windows窗体和texbox的脚本允许用户查找和替换未嵌入的文本文件中的文本。





How do I read using Streamreader an embedded resource (txt file) and return it as a string? My current script using a windows form and texbox allows the user to find and replace text in a text file that is not embedded.


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

        private void button1_Click(object sender, EventArgs e)
        {
            StringCollection strValuesToSearch = new StringCollection();

            strValuesToSearch.Add("Apple");

            string stringToReplace;

            stringToReplace = textBox1.Text;



            StreamReader FileReader = new StreamReader(@"C:\MyFile.txt");

            string FileContents;

            FileContents = FileReader.ReadToEnd();

            FileReader.Close();

            foreach (string s in strValuesToSearch)
            {

                if (FileContents.Contains(s))

                    FileContents = FileContents.Replace(s, stringToReplace);

            }

            StreamWriter FileWriter = new StreamWriter(@"MyFile.txt");

            FileWriter.Write(FileContents);

            FileWriter.Close();
        }

       
    }
}

推荐答案

如果你曾经回来,我希望你这样做:



以下代码片段来自: http://www.dottodotnet.com/2010/10/read-embedded-resource-text-file-in-c.html



If you ever do come back, and I hope you do:

The following snippet is from here: http://www.dottodotnet.com/2010/10/read-embedded-resource-text-file-in-c.html

internal string GetFromResources(string resourceName)
        {
            Assembly assem = this.GetType().Assembly;
            using (Stream stream = assem.GetManifestResourceStream(resourceName))
            {
                using (var reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }


如果你的类是静态类,那么使用这个方法:

If your class is static class then use this methods:
internal static string GetFromResources(string resourceName)
        {
            Assembly assem = Assembly.GetExecutingAssembly();

            using (Stream stream = assem.GetManifestResourceStream(assem.GetName().Name + '.' + resourceName))
            {
                using (var reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }







例如,如果你的此项目中的嵌入式资源文件名为'MyFile.txt',然后使用此静态方法,此代码相同:




For example if your embedded resource file names on the this project is 'MyFile.txt' then use this static method same this code:

var myFileData = GetFromResources("MyFile.txt");


尝试此处


这篇关于C#如何读取嵌入式资源并以字符串形式返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆