C# File Operations: Part 1

In this article we will see how we can write our data into a file & how we can append data into a file.

System.IO.FileStream myFileStream = new System.IO.FileStream("C:\\myTemp.txt", System.IO.FileMode.CreateNew, System.IO.FileAccess.ReadWrite);

Using above line it will create a new object of the FileStream & in that we will pass the filename with path in which we want to write something or append something.

After that give a FileMode, here we will give it "CreateNew". Using this value it will create a new file but if the file is already open then it will throw an exception.

After that we will pass the File Access value as ReadWrite.

System.IO.StreamWriter myStreanWriter = new System.IO.StreamWriter(myFileStream);

The above line will create an object of the StreamWriter class and in that passing an object of the FileStream Class.

System.IO.SeekOrigin.Begin

The above line will set the pointer to the beginning of the file, because we want to write our data to the new file.

 

System.IO.SeekOrigin.End

Using the above line will set the pointer at the end of the file, using this you can append the data into an already exist file.

 

Main Code :
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MyBlog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 
           
btnWriteFile.Click += new EventHandler(btnWriteFile_Click);
            btnAppendFile.Click += new EventHandler(btnAppendFile_Click);          
       
}

        private void btnWriteFile_Click(object sender, EventArgs e)
        {
            try
            {
                System.IO.FileStream myFileStream
 = new System.IO.FileStream("C:\\myTemp.txt", System.IO.FileMode.CreateNew, System.IO.FileAccess.ReadWrite);
                System.IO.StreamWriter myStreanWriter = new System.IO.StreamWriter(myFileStream);
                myStreanWriter.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);
                myStreanWriter.Write("Hello");
                MessageBox.Show("Your Data Written Successfully.");

                myStreanWriter.Close();
                myFileStream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
       }

        private void btnAppendFile_Click(object sender, EventArgs e)
        {
           try
            {
                System.IO.FileStream myFileStream=new System.IO.FileStream("C:\\myTemp.txt",System.IO.FileMode.Append,System.IO.FileAccess.Write);
                System.IO.StreamWriter myStreamWriter = new System.IO.StreamWriter(myFileStream, System.Text.Encoding.Default);
                myStreamWriter.BaseStream.Seek(0, System.IO.SeekOrigin.End);
                myStreamWriter.Write("Append");
                MessageBox.Show("Your Data Appended Successfully.");

                myStreamWriter.Close();
                myFileStream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
       
}
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all