C# FileStream WriteAsync(Byte[], Int32, Int32)

In this chapter you will learn:

  1. Get to know FileStream.WriteAsync(Byte[], Int32, Int32)
  2. Syntax for FileStream.WriteAsync(Byte[], Int32, Int32)
  3. Parameter for FileStream.WriteAsync(Byte[], Int32, Int32)
  4. Returns for FileStream.WriteAsync(Byte[], Int32, Int32)
  5. Example - FileStream.WriteAsync(Byte[], Int32, Int32)

Description

FileStream WriteAsync(Byte[], Int32, Int32) Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

Syntax

FileStream.WriteAsync(Byte[], Int32, Int32) has the following syntax.


[ComVisibleAttribute(false)]//from w w w .  ja  va  2  s .  c  o  m
[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public Task WriteAsync(
  byte[] buffer,
  int offset,
  int count
)

Parameters

FileStream.WriteAsync(Byte[], Int32, Int32) has the following parameters.

  • buffer - The buffer to write data from.
  • offset - The zero-based byte offset in buffer from which to begin copying bytes to the stream.
  • count - The maximum number of bytes to write.

Returns

FileStream.WriteAsync(Byte[], Int32, Int32) method returns A task that represents the asynchronous write operation.

Example


using System;//from w  w  w.j  a  v a2  s . c o  m
using System.Text;
using System.Threading.Tasks;
using System.IO;

public class MainWindow 
{
    public MainWindow()
    {
        UnicodeEncoding uniencoding = new UnicodeEncoding();
        string filename = @"c:\Users\exampleuser\Documents\userinputlog.txt";

        byte[] result = uniencoding.GetBytes(UserInput.Text);

        using (FileStream SourceStream = File.Open(filename, FileMode.OpenOrCreate))
        {
            SourceStream.Seek(0, SeekOrigin.End);
            await SourceStream.WriteAsync(result, 0, result.Length);
        }
    }
    public static void Main(){}
}

Next chapter...

What you will learn in the next chapter:

  1. Get to know FileStream.WriteByte
  2. Syntax for FileStream.WriteByte
  3. Parameter for FileStream.WriteByte
  4. Returns for FileStream.WriteByte
  5. Example - FileStream.WriteByte