• [原]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.程序源码下载

    作者:a497785609 发表于2014-6-3 20:49:14 原文链接
    阅读:19 评论:0 查看评论
  • 相关阅读:
    System.nanoTime()的使用
    只为高效、流畅开发 —— 码云企业版 3.0 倾情上线
    不自律的人,下场都很惨
    刘德:小米已投89家生态链企业 有品要做百亿电商平台(本质上是是利用了小米的大火炉的余热,但也有反向的正面作用)
    英雄无敌手游(战争纪元云中城,还可以骑龙,绝美)
    openFrameworks 是一个旨在助力你进行开创性工作的开源 C++ 工具箱(是很多其它类库的组合)
    Core开发-MVC 使用dotnet 命令创建Controller和View
    Ant Table组件
    web性能优化
    scss + react + webpack + es6
  • 原文地址:https://www.cnblogs.com/zhangqs008/p/3773642.html
Copyright © 2020-2023  润新知