好的,相信我,我已经研究过,并向几个朋友询问了这个问题。但我的代码:http://pastebin.com/0JSA9MzA在那里。指定的两行之间的代码应该逐行检查由startLine变量指定的文本行的文本文档,然后它应该改变从oldFont变量到newFont变量的所有内容,然后在endLine结束变量。但每当我运行它时,它都无法产生结果,并且不会产生任何错误。
答案 0 :(得分:2)
除非您告诉它更改文件中的文本,否则您的代码将无法更改文件中的文本。您目前只修改您读入的文本,但您从未将其写回文件。
答案 1 :(得分:0)
试试这个:
private void editText()
{
System.IO.StreamReader read = new System.IO.StreamReader(filePath);
//write to new file because it's not possible to replace text in existing file directly
System.IO.TextWriter writer = new System.IO.TextWriter(filePath+"_");
while ((line = read.ReadLine()) != null)
{
writer.WriteLine(line); //write unmodified line to new file
if (startLine == line)
{
while ((line = read.ReadLine()) != null)
{
if (line == endLine)
{
writer.WriteLine(line); //write unmodified line to new file
break; //exit cycle
}
else
{
line = line.Replace(oldFont, newFontS); //replace content in line
writer.WriteLine(line); //write modified line to new file
}
}
}
}
read.Close();
writer.Close();
System.IO.File.Delete(filePath); //delete old file
System.IO.File.Move(filePath+"_",filePath); //rename new file to original filename
MessageBox.Show("Done!");
}