PCL Storage append text to a file.

Hello,
I try to append some text to an existing file, but I don't know how?

The file.WriteAllTextAsync(TEXT) overwrittes all old text:

var folder = await this.rootFolder.GetFolderAsync(folderName).ConfigureAwait(false); var file = await folder.GetFileAsync(fileName).ConfigureAwait(false); await file.WriteAllTextAsync(text).ConfigureAwait(false);

I have also tried to use the stream to append text, but I can't find out, how I can do this. I've tried it with the following code, I think the offset is the problem, isn't it?

var folder = await this.rootFolder.GetFolderAsync(folderName).ConfigureAwait(false); var file = await folder.GetFileAsync(fileName).ConfigureAwait(false); var bufferArray = Encoding.UTF8.GetBytes(text); using (var streamToWrite = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false)) { if (streamToWrite.CanWrite) { await streamToWrite.WriteAsync(bufferArray, 0, bufferArray.Length).ConfigureAwait(false); } }

Can someone help?

Best Answer

  • Vlign_PhiltzVlign_Philtz US ✭✭
    Accepted Answer

    I have solved the issue by myself:

    var folder = await this.rootFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
    var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
    var bufferArray = Encoding.UTF8.GetBytes(text);
    using (Stream streamToWrite = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false))
    {
       streamToWrite.Position = streamToWrite.Length;
       if (streamToWrite.CanWrite)
       {
          await streamToWrite.WriteAsync(bufferArray, 0, bufferArray.Length).ConfigureAwait(false);
       }
    }
    

    I need to set the position of the stream to the end of the stream. I've done this with length property. Thank's at all.

Answers

  • Vlign_PhiltzVlign_Philtz USMember ✭✭
    Accepted Answer

    I have solved the issue by myself:

    var folder = await this.rootFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
    var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists).ConfigureAwait(false);
    var bufferArray = Encoding.UTF8.GetBytes(text);
    using (Stream streamToWrite = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false))
    {
       streamToWrite.Position = streamToWrite.Length;
       if (streamToWrite.CanWrite)
       {
          await streamToWrite.WriteAsync(bufferArray, 0, bufferArray.Length).ConfigureAwait(false);
       }
    }
    

    I need to set the position of the stream to the end of the stream. I've done this with length property. Thank's at all.

  • jerryno6jerryno6 VNMember ✭✭

    Thanks for your post.

  • batmacibatmaci DEMember ✭✭✭✭✭

    are you writting this in Pcl project ? do you use pcl storage plugin? if yes, i have heard that it has many issues and it doensnt support .net standard yet

  • Vlign_PhiltzVlign_Philtz USMember ✭✭

    Yes, I use it in PCL with .Net Standard. It works great for me.

  • MrLovaMrLova USMember ✭✭

    hey,

    is there a possibility like WriteLineAsync i want to write in a new line ?

    thanks

  • MrLovaMrLova USMember ✭✭

    i try this one but still continue write in a line:

                    byte[] newline = Encoding.UTF8.GetBytes(Environment.NewLine);
                    await streamToWrite.WriteAsync(newline, 0, newline.Length).ConfigureAwait(false);
    
Sign In or Register to comment.