-4

好吧,相信我,我到处研究过,问过几个朋友这个问题。但我的代码: http: //pastebin.com/0JSA9MzA在那里。指定的两行之间的代码应该逐行检查文本文档中由 startLine 变量指定的文本行,然后应该将所有内容从 oldFont 变量更改为 newFont 变量,然后在 endLine 结束多变的。但是每当我运行它时,它都无法产生结果,并且没有给出任何错误。

4

2 回答 2

2

好吧,除非您告诉它更改文件中的文本,否则您的代码不会更改文件中的文本。您当前仅修改您读入的文本,但您永远不会将其写回文件。

于 2013-07-03T23:01:10.580 回答
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!");
    }
于 2013-07-03T23:32:32.273 回答