试图替换文本文件中的字符串但不起作用

时间:2013-08-08 20:36:11

标签: c#

class FunWithScheduling
{
        static void Main()
        {
                StringBuilder newFile = new StringBuilder();
                string temp = "";
                string[] file = File.ReadAllLines(Scheduler.txt");
                foreach (string line in file)
                {
                            if (line.Contains("Subhadra"))
                            {
                                   temp = line.Replace("Subhadra", "Thangam");
                                   newFile.Append(temp + "\r\n");
                                   continue;
                             }
                             newFile.Append(line + "\r\n");
                }
                File.WriteAllText("Scheduler.txt", newFile.ToString());
            }
         }

1 个答案:

答案 0 :(得分:3)

您错过了以下行中的开头报价:

string[] file = File.ReadAllLines(Scheduler.txt");

因此,编译器正在读取之后的所有结尾引号作为字符串常量的开头。

为了解决这个问题,你需要将上面一行更改为下面的行,其中有一个开头的引号: -

string[] file = File.ReadAllLines("Scheduler.txt");