将新字符串添加到文件的最后一行

sok*_*sok 1 c#

就像tittle说的那样,我想在文件底部添加一个新字符串,但不知怎的,它不起作用.希望有人可以帮助我x___x

      private string add(string asd){
        {string filename = "asd.txt";
        StreamReader reader = new StreamReader(filename);
        StreamWriter write = new StreamWriter(filename);
        string input = null;
        while ((input = reader.ReadLine()) != null)
        {
            write.WriteLine(input);
        }
        reader.Close();
        write.WriteLine(asd);
        write.Close();}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 7

使用File.AppendAllText.

打开文件,将指定的字符串附加到文件,然后关闭该文件.如果该文件不存在,则此方法创建文件,将指定的字符串写入文件,然后关闭该文件.

例:

private string Add(string asd) {
    string filename = "asd.txt";
    File.AppendAllText(filename, asd);
}
Run Code Online (Sandbox Code Playgroud)