Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp C# Read File Content

C# Read File Content

This post is simple a function and example on How to read content of a text file by using C#. This file could be .txt, .xml, .log which is generated by your application/web for caching or storing user information or parsing data used in your application.

In function below, we need to use System.IO namespace to utilize functions in FileStream and StreamReader classes. Also we need to use the MapPath method to get the physical path of the file we want to read its content.

Read File Content With C#

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
 
public partial class read_file : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string filename = "Hehe.htm";
        Response.Write(ReadFromFile(filename));
    }
 
    public static string ReadFromFile(string filename)
    {
        filename = HttpContext.Current.Server.MapPath(filename);
 
        string s = "";
        try
        {
            FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(file);
            s = sr.ReadToEnd();
            sr.Close();
            file.Close();
        }
        catch
        {
            s = "";
        }
        return s;
    }
}

using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; public partial class read_file : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string filename = "Hehe.htm"; Response.Write(ReadFromFile(filename)); } public static string ReadFromFile(string filename) { filename = HttpContext.Current.Server.MapPath(filename); string s = ""; try { FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(file); s = sr.ReadToEnd(); sr.Close(); file.Close(); } catch { s = ""; } return s; } }

Dec 29, 2011Hoan Huynh
Facebook Removed View App Profile Page link For New AppsHide/ Disable Chat and SMS box in Gmail
You Might Also Like:
  • PHP Read File Using Fread() Simple Example
  • PHP Save String Content To File
  • ASP.Net C# Download Or Save Image File From URL
  • Get Or Read AppSettings Item Value From The Web.Config File
  • Get File Mime Type Using PHP
  • C# Get File Extension Without Sub String Or Split Function
  • C# Check File Extension In Array Of Valid Extension
  • PHP Show Limited Words Of A String Content Based On Max Length
  • PHP Get Remote File Size And Content Type
  • ASP.NET C# Export Data To CSV And Prompt Download
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago CSharpFileStream, MapPath, ReadToEnd, StreamReader262
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,209 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,077 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,854 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,334 views
C# Read Json From URL And Parse/Deserialize Json
9,828 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Photo Editor App – How Good Is it?

  • The Best Way To Write An Essay – Creating A Good Essay
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (114)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development