如何更新文本文件中的行?

时间:2013-04-26 03:08:36

标签: c#

我想更新文本文件中的某个部分/行..

我有这段代码,但它不起作用:

public static void UpdatingMethod()
{
    string text = File.ReadAllText("fileone.txt");
    text = text.Replace("old value", "new stuff");
    File.WriteAllText("fileone.txt", text);
}

public static void WritingMethod()
{
    int count = 0;
    while (count < 10)
    {
        Console.Write(" Enter your Name: ");
        Name = Console.ReadLine();

        Console.Write(" Enter your ID: ");
        ID = int.Parse(Console.ReadLine());

        Console.Write(" Enter your Age: ");
        Age = int.Parse(Console.ReadLine());

        Console.Write(" Enter your E-mail: ");
        Email = Console.ReadLine();

        StreamWriter Sw = new StreamWriter("fileone.txt", true);
        string output = string.Format("Thank you for registration! Your Submitted information are:" + Environment.NewLine + "Name: {0}"
        + Environment.NewLine + "ID: {1}" + Environment.NewLine + "Age: {2}" + Environment.NewLine + "E-mail: {3}", Name, ID, Age, Email);           
        Console.WriteLine(output);      
        Sw.WriteLine(output + Environment.NewLine);
        Console.ReadLine();

        Sw.Close();
        count++;
    }
}

1 个答案:

答案 0 :(得分:2)

您需要有一个main方法,用作应用程序的入口点。

class TestClass
{
    static void Main(string[] args)
    {
        // not really good names for what it's worth
        WritingMethod();
        UpdatingMethod();
    }
}

MSDN Main() & Command Line arguments