Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# move to end of file with StreamWriter created from FileStream

Tags:

c#

filestream

I need to create a StreamWriter from a FileStream object and append some text to the file. It is assumed that the FileStream object that is being used has been created with FileMode.OpenOrCreate and FileAccess.ReadWrite. I have:

using (FileStream fs = GetCurrentFileStream())
{
    StreamWriter sw = new StreamWriter(fs);
    sw.WriteLine("StringToAppend");
    sw.Flush();
}

However this just overwrites the file from the beginning. How do I move to the end of the file? Is there perhaps a way to change the FileMode to Append and FileAccess to Write after the FileStream has been created?

Edit: As mentioned above I need to do this using a FileStream object. The answers from Open existing file, append a single line assume that I can create a new StreamWriter from the file path which I don't have.

Edit 2: Added truncated version of GetCurrentFileStream().

    public static FileStream GetCurrentFileStream()
    {
        String fileName = getFileName();
        FileStream fs = OpenFileWhenAvailable(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
    }

    public static FileStream OpenFileWhenAvailable(String fileName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
    {
        int tries = 0;
        int timeout = 10 * 1000;
        while (true)
        {
            tries++;
            try
            {
                return new FileStream(fileName, fileMode, fileAccess, fileShare);
            }
            catch (IOException ex)
            {
                if (tries * 100 > timeout)
                {
                    return null;
                }
                else
                {
                    System.Threading.Thread.Sleep(100);
                }
            }
        }
    }

GetCurrentFileStream is used in several different contexts, so changing the FileMode and FileAccess directly is not an option. I do not wish to make a separate version of GetCurrentFileStream just for this one case, which is why I'm asking if there is a way to jump to the end of the stream and append a string when the FileStream object has already been created.

like image 384
EJS Avatar asked Feb 08 '23 14:02

EJS


1 Answers

If I understood correctly, you want to append your line to a created file:

using (FileStream fs = GetCurrentFileStream())
{
    StreamWriter sw = new StreamWriter(fs, true);
    sw.WriteLine("StringToAppend");
    sw.Flush();
}

With this overload of the StreamWriter constructor you choose if you append the file, or overwrite it.

It will be really cool if you show your implementation of method GetCurrentStream():

using (FileStream fileStream = new FileStream(fileName,FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
   sw.WriteLine(something);
}

Update:

using (FileStream fs = GetCurrentFileStream())
{
   StreamWriter sw = new StreamWriter(fs);
   long endPoint=fs.Length;
   // Set the stream position to the end of the file.        
   fs.Seek(endPoint, SeekOrigin.Begin);
   sw.WriteLine("StringToAppend");
   sw.Flush();
 }
like image 52
StepUp Avatar answered Feb 15 '23 09:02

StepUp