Question

I have following code for log file which will create a log file and write log into that file.

But I want to write that log into file in DESC order so that recent log text will come first.

 string FilePath = Path(MYLOG.txt");

                if (!File.Exists(FilePath))
                {
                    byte[] fileBytes = null;
                    fileBytes = Encoding.GetEncoding(1252).GetBytes("My Log -\n");
                    using (Stream streamToWrite = File.Create(FilePath))
                    {
                        streamToWrite.Write(fileBytes, 0, fileBytes.Length);
                        streamToWrite.Flush();
                    }

I mean want to write new content from start not to end of the file

Was it helpful?

Solution

I think this will do

string currentContent = String.Empty;
            if (File.Exists(filePath))
            {
                currentContent = File.ReadAllText(filePath);
            }
            File.WriteAllText(filePath, newContent + currentContent );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top