c# читаем последнюю строку и удаляем её / Get last line from file and delete it

private static string ReadDeleteHashFromFile(string MyFilePath)
{
            string lastline = "";
            using (StreamReader reader = new StreamReader(dumpFilePath))
            {
                using (StreamWriter writer = new StreamWriter("myfile.tmp"))
                {
                    string line = reader.ReadLine();
                    
                    while (!reader.EndOfStream)
                    {
                        writer.Write(line);
                        line = reader.ReadLine();
                        if (!reader.EndOfStream)
                            writer.Write(writer.NewLine);
                    }
                    lastline = line;
                }
            }
            File.Delete(MyFilePath);
            File.Move("myfile.tmp", MyFilePath);
            File.Delete("myfile.tmp");
            return lastline;
}

No comments:

Post a Comment