(Visual Studio C# 2010,Windows 窗体应用程序)嘿,我想替换 .txt 文件中的一些单词。我知道该怎么做,但我也不知道如何保留原始文件。我想将修改后的文本输出到与输入文件不同名称的文件中。否则,没有简单的方法进行比较,因为原始文件被覆盖......
这就是我到目前为止所得到的。
this.openFileDialog1.Filter =`enter code here`"TEXT (*.xml;*.txt|";
this.openFileDialog1.Multiselect = true;
this.openFileDialog1.Title = "My text editor";
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
foreach (String file in openFileDialog1.FileNames)
{
try
{
StreamReader reader = new StreamReader(file);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, "play", "player");
content = Regex.Replace(content, "game", "gamer");
content = Regex.Replace(content, "walk", "walking");
StreamWriter writer = new StreamWriter(file);
writer.Write(content); writer.Close();
}
catch
{ // The user lacks appropriate permissions to read files, discover paths, etc. MessageBox.Show("Security error. Please contact your administrator for details.\n\n" + "Error message: Not found" ); } {
}
}
}