Mega Code Archive

 
Categories / C# / File Stream
 

Read all the content from a file as string in default encoding

//http://tinyerp.codeplex.com/ //GNU Library General Public License (LGPL) //----------------------------------------------------------------------- // <copyright file="SysUtil.cs" company="Pyramid Consulting"> //     Copyright (c) Pyramid Consulting. All rights reserved. // </copyright> //----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Text; namespace Bamboo.Core.Common {     public class SysUtil     {         /// <summary>         /// read all the content from a file as string in default encoding         /// </summary>         /// <param name="strFilePath">source file path</param>         /// <returns>dest string in default encoding</returns>         public static String ReadFile(String strFilePath)         {             System.Text.Encoding encDefault = System.Text.Encoding.GetEncoding(0);             System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);             System.IO.BinaryReader br = new System.IO.BinaryReader(fs);             String strResult = null;             try             {                 byte[] bData = new byte[fs.Length];                 br.Read(bData, 0, bData.Length);                 strResult = encDefault.GetString(bData);             }             finally             {                 br.Close();                 fs.Close();             }             return strResult;         }     } }