C# Winform 文件编码批量转换工具

在发布产品程序包时,往往需要对程序文件进行编码检查,写了一个可以批量修改文件编码格式的辅助工具,希望对有同样需求的童鞋有帮助。
1.程序界面:
2.核心代码:
        /// <summary>
        /// 取得文件编码方式
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public Encoding GetFileEncode(string path)
        {
            FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
            var buffer = new byte[fileStream.Length];
            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="buffer"></param>
        /// <returns></returns>
        private 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="buffer"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        private 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>
        private string GetTxt(byte[] buffer)
        {
            return GetTxt(buffer, GetEncode(buffer));
        }

        /// <summary>
        /// 写入文本
        /// </summary>
        /// <param name="filepath">写入文件</param>
        /// <param name="body">写入内容</param>
        /// <param name="encoding">编码方式</param>
        private void WriteTxt(string filepath, string body, Encoding encoding)
        {
            if (File.Exists(filepath))
                File.Delete(filepath);
            byte[] bytes = encoding.GetBytes(body);
            FileStream fileStream = File.Open(filepath, FileMode.CreateNew, FileAccess.Write);
            if (Equals(encoding, Encoding.UTF8))
            {
                fileStream.WriteByte(239);
                fileStream.WriteByte(187);
                fileStream.WriteByte(191);
            }
            else if (Equals(encoding, Encoding.BigEndianUnicode))
            {
                fileStream.WriteByte(254);
                fileStream.WriteByte(byte.MaxValue);
            }
            else if (Equals(encoding, Encoding.Unicode))
            {
                fileStream.WriteByte(byte.MaxValue);
                fileStream.WriteByte(254);
            }
            fileStream.Write(bytes, 0, bytes.Length);
            fileStream.Flush();
            fileStream.Close();
            fileStream.Dispose();
        }

3.程序源码下载

posted @ 2014-06-03 20:49  清山博客  阅读(2023)  评论(0编辑  收藏  举报