Click here to Skip to main content
15,908,455 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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();
        }

       
    }
}
Posted
Updated 22-Jul-10 12:46pm
v2
Comments
GuyThiebaut 21-Oct-18 8:57am    
One extra thing that can help is to run the following to get the resources available first if you need to figure out the path of the resource you are looking for:
var resources = assem.GetManifestResourceNames();

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();
                }
            }
        }
 
Share this answer
 
Comments
Member 13655521 8-Feb-18 4:11am    
I have been trying to fix my issues and after literal hours of searching your comment saved my week. I dont even know how to thank you enough right now. Thank you good sir!
If your class is static class then use this methods:
C#
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();
                }
            }
        }



For example if your embedded resource file names on the this project is 'MyFile.txt' then use this static method same this code:
C#
var myFileData = GetFromResources("MyFile.txt");
 
Share this answer
 
v2
Try here.
 
Share this answer
 
Comments
elton123 23-Jul-10 9:38am    
Ok. I'll go elsewhere.
mikehell68 22-Mar-13 12:08pm    
erm.. circular reference. That's how most people get here in the first place.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900