Home
Manage Your Code
Snippet: Get File contents into String (C#)
Title: Get File contents into String Language: C#
Description: This code is used to read contents of a given file. Views: 155
Author: Dhaval Patel Date Added: 5/6/2008
Copy Code  
1    public static string GetFileContents(string FileName)
2    {
3        StreamReader sr = null;
4        string FileContents = null;
5
6        try
7        {
8            FileStream fs = new FileStream(FileName, FileMode.Open,
9                                           FileAccess.Read);
10            sr = new StreamReader(fs);
11            FileContents = sr.ReadToEnd();
12        }
13        finally
14        {
15            if (sr != null)
16                sr.Close();
17        }
18
19        return FileContents;
20    }