文章分类

相关文章

C#文件读写辅助类(FileHelper)

分类:C#     发布:2018-09-27     来源:本站     浏览:135 次
//--------------------------------------------------------------------------------
// 文件描述:文件读写辅助类
// 文件作者:全体开发人员
// 创建日期:2016-06-27
//--------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace RC.Framework
{
    /// <summary>
    /// 文件读写辅助类
    /// </summary>
    public class FileHelper
    {
        private const double KbCount = 1024;
        private const double MbCount = KbCount*1024;
        private const double GbCount = MbCount*1024;
        private const double TbCount = GbCount*1024;
       
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string ReadFile(string filePath)
        {
            if (File.Exists(filePath))
            {
                var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                using (var sr = new StreamReader(fs, Encoding.UTF8))
                {
                    return sr.ReadToEnd();
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 取得文件编码
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Encoding GetFileEncoding(string path)
        {
            var fileStream = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
            var buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, buffer.Length);
            fileStream.Close();
            fileStream.Dispose();
            var fileEncoding = GetEncode(buffer);
            return fileEncoding;
        }

        /// <summary>
        /// 自适应编码读取文本
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetTxt(string path)
        {
            var fileStream = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
            var buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, buffer.Length);
            fileStream.Close();
            fileStream.Dispose();
            return GetTxt(buffer, GetEncode(buffer));
        }

        /// <summary>
        /// 按指定编码方式读取文本
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string GetTxt(byte[] buffer, Encoding encoding)
        {
            if (Equals(encoding, Encoding.UTF8))
                return encoding.GetString(buffer, 3, buffer.Length - 3);
            if (Equals(encoding, Encoding.BigEndianUnicode) || Equals(encoding, Encoding.Unicode))
                return encoding.GetString(buffer, 2, buffer.Length - 2);
            return encoding.GetString(buffer);
        }

        /// <summary>
        /// 取得文件编码方式
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Encoding GetEncode(byte[] buffer)
        {
            if (buffer.Length <= 0 || buffer[0] < 239)
                return Encoding.Default;
            if (buffer[0] == 239 && buffer[1] == 187 && buffer[2] == 191)
                return Encoding.UTF8;
            if (buffer[0] == 254 && buffer[1] == byte.MaxValue)
                return Encoding.BigEndianUnicode;
            if (buffer[0] == byte.MaxValue && buffer[1] == 254)
                return Encoding.Unicode;
            return Encoding.Default;
        }

        /// <summary>
        /// 写入文本
        /// </summary>
        /// <param name="filepath">写入
    

上一篇:ASP.NET 读写Webconfig的AppSetting和ConnectionString

下一篇:1.1 正则基础-基本概念