How to read embedded resource text file
-
How do I read using Streamreader an embedded resource (txt file)? (and return it as a string?) My current script uses a windows form and texbox that allows the user to find and replace text in a text file that is not embedded. 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(); }
-
Answer:
You can use the http://msdn.microsoft.com/en-us/library/xc4235zt%28v=VS.85%29.aspx: using (Stream stream = Assembly.GetExecutingAssembly() .GetManifestResourceStream("file1.txt")) using (StreamReader reader = new StreamReader(stream)) { string result = reader.ReadToEnd(); }
Me.Close at Stack Overflow Visit the source
Other answers
Take a look at this page: http://support.microsoft.com/kb/319292 Basically, you use System.Reflection to get a reference to the current Assembly. Then, you use GetManifestResourceStream(). Example, from the page I posted: Note: need to add using System.Reflection; for this to work Assembly _assembly; StreamReader _textStreamReader; try { _assembly = Assembly.GetExecutingAssembly(); _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt")); } catch { MessageBox.Show("Error accessing resources!"); }
SimpleCoder
When you added the file to the resources you should select it's Access Modifiers as public than you can make something like following. byte[] clistAsByteArray = Properties.Resources.CLIST01; CLIST01 is the name of the embedded file. Actually you can go to the resources.Designer.cs and see what is the name of the getter.
Night Walker
Related Q & A:
- How to read csv file using php?Best solution by Stack Overflow
- How to read UTF-8 .txt file in Android?Best solution by wiki.mcneel.com
- How to use hadoop for text file?Best solution by Stack Overflow
- How to delete row from text file?Best solution by Stack Overflow
- How to extract information from text file in Python?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.