How to use the FileStream class to write contents to a file in C#

Category > CSHARP || Published on : Friday, October 9, 2020 || Views: 657 || FileStream class to write contents to a file in C#


Here Pawan Kumar will explain how to FileStream class to write contents to a file in C#

FileStream class to write contents to a file in C#

using System;
using System.IO;
namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream src = new
            FileStream(@"C:\test.txt", FileMode.Open, FileAccess.Write);
            string str = "Hello World";
            byte[] bytes = new byte[str.Length];
            bytes = System.Text.Encoding.UTF8.GetBytes(str);

            src.Write(bytes, 0, str.Length);
            Console.Read();
        }
    }
}