Home
Manage Your Code
Snippet: ReadTextFile (C#)
Title: ReadTextFile Language: C#
Description: Read a text file. Return all the lines. If the file doesn't exist, return string.Empty Views: 143
Author: andre piar Date Added: 12/27/2007
Copy Code  
1public static string ReadTextFile(string Path)
2{
3    if (System.IO.File.Exists(Path))
4    {
5        System.IO.StreamReader sr = null;
6        try
7        {
8            sr = new System.IO.StreamReader(Path);
9            return sr.ReadToEnd();
10        }
11        catch
12        {
13            return string.Empty;
14        }
15        finally
16        {
17            if (sr != null)
18                sr.Close();
19        }
20    }
21    else
22        return string.Empty;
23}