Reads text from a file. : Text File Read Write « File Stream « C# / C Sharp






Reads text from a file.

     
/*
 * Filename:    FileUtil.cs
 * Product:     Versioning Controlled Build
 * Solution:    BuildAutoIncrement
 * Project:     Shared
 * Description: File utility methods.
 * Copyright:   Julijan ?ribar, 2004-2007
 * 
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the author(s) be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace BuildAutoIncrement {

    /// <summary>
  /// A set of file utilities.
  /// </summary>
  public struct FileUtil {
        /// <summary>
        ///   Reads text from a file.
        /// </summary>
        /// <param name="fileName">
        ///   Filename.
        /// </param>
        /// <param name="encoding">
        ///   <c>out</c> parameter with the encoding detected.
        /// </param>
        /// <returns>
        ///   Content of the file.
        /// </returns>
        public static string ReadTextFile(string fileName, out Encoding encoding) {
      Debug.Assert(fileName != null);
      Debug.Assert(File.Exists(fileName));
            encoding = Encoding.Default;
            using (StreamReader sr = new StreamReader(File.OpenRead(fileName), encoding, true)) {
                string content = sr.ReadToEnd();
                encoding = sr.CurrentEncoding;
                return content;
            }
    }

  }
}

   
    
    
    
    
  








Related examples in the same category

1.Read and Write a Text File
2.Read text file line by line
3.Read ASCII string from byte bufferRead ASCII string from byte buffer
4.Reads and displays bytes until end-of-fileReads and displays bytes until end-of-file
5.Write string to a text file
6.Read whole text file to the end
7.Read text file line by line with exception catch
8.Text file Write with format and write boolean value to a text file
9.Action Text Reader Line
10.Open and Append to a Log File
11.Read and Write to a Newly Created Data File
12.Read Text from a File
13.Read text file with File.OpenText
14.Write Text to a File
15.Read a text file and obtain it's contents.
16.Saves the text to a file.
17.Read Text File From Ressource
18.Create a large file of 100 lines to upload
19.Creates or opens a file for writing and writes text to it.
20.Returns the raw number of the current line count.
21.Returns the zero-based line number where source appears in target.
22.Returns the number of lines appearing in target where a line is counted as a '\n'