Follow in Twitter & Facebook



Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools



Thursday, October 18, 2012

File Read Write in C#.Net

In this article we will discuss about how to create a text file in C#.Net using System.IO namespace as well as how we can read from the text file in C#.Net. You can check my previous article on how to get driver info in C#.Net, how to add users to group in active directory in C#.Net, as well as you can check some SQL Server articles.

C#.Net provides system.IO namespace to work with files and directory. Here we will check how we can read and write to text file using Streams.

We can Write to a file and Read from a file using StreamWriter and  StreamReader.

Below is the full code:

HTML Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadAndWriteTextFile.aspx.cs"
    Inherits="ReadAndWriteTextFile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Read and Write text file in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Create Text File:
        <asp:Button ID="btnCreateTextFile" runat="server" Text="Create Text File"
            onclick="btnCreateTextFile_Click" /><br />
        Read From Text File: <asp:Button ID="btnReadFromTextFile"
            runat="server" Text="Read from Text File" onclick="btnReadFromTextFile_Click" />
        <br />
        <asp:Label ID="lblReadFile" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

.cs Code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

public partial class ReadAndWriteTextFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
 
    protected void btnCreateTextFile_Click(object sender, EventArgs e)
    {
        CreateTextFile();
    }
    protected void btnReadFromTextFile_Click(object sender, EventArgs e)
    {
        ReadFromTextFile();
    }



    void CreateTextFile()
    {
        try
        {
            StreamWriter sw;
            sw = File.CreateText(@"c:\myTestfile.txt");
            sw.WriteLine("We have successfully created a text file in C drive !!!");
            sw.WriteLine("Thanks to SharePointDotNet.com !!!");
            sw.Close();
        }
        catch (Exception ex)
        {
            //Hande the exception here !
        }
    }
If you will check C drive then you will get the text file as shown in the figure below:

    void ReadFromTextFile()
    {
        try
        {
            FileStream fs = File.Open(@"c:\myTestfile.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader sr = new StreamReader(fs);
            StringBuilder sb = new StringBuilder();
            sb.Append(sr.ReadToEnd());
            lblReadFile.Text = sb.ToString();
        }
        catch (Exception ex)
        {
            //Hande the exception here !
        }
    }
}

Once you will click on Read From Text file button, you will be able to see like below:




0 on: "File Read Write in C#.Net"