如何使用StreamReader
读取嵌入资源(文本文件)并将其作为字符串返回?我当前的脚本使用Windows表单和文本框,允许用户在未嵌入的文本文件中查找和替换文本。
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();}