美文网首页
c# 读取文本文件编码格式最安全简单的方式

c# 读取文本文件编码格式最安全简单的方式

作者: HaRun | 来源:发表于2020-07-14 22:06 被阅读0次

用此库:c# 读取文本文件编码格式使用 ude, https://github.com/errepi/ude,可以使用nuget 安装此包,搜索“ude”即可

使用示例

            string filePath = "";

            //filePath = @"C:\Users\Admin\Downloads\testEncoding\GB2312.txt";
            //filePath = @"C:\Users\Admin\Downloads\testEncoding\GBK.txt";
            // filePath = @"C:\Users\Admin\Downloads\testEncoding\utf-8.txt";
            // filePath = @"C:\Users\Admin\Downloads\testEncoding\utf-8-with-bom.txt";
            filePath = @"C:\Users\Admin\Downloads\testEncoding\utf-16.txt";
  
            using (FileStream fs = File.OpenRead(filePath))
            {
                Ude.CharsetDetector cdet = new Ude.CharsetDetector();
                cdet.Feed(fs);
                cdet.DataEnd();
                if (cdet.Charset != null)
                {
                    Debug.WriteLine("Charset: {0}, confidence: {1}",
                         cdet.Charset, cdet.Confidence);

                    Encoding encoding = Encoding.GetEncoding(cdet.Charset);

                    string content = System.IO.File.ReadAllText(filePath, encoding);

                    Debug.WriteLine(content);
                }
                else
                {
                    Debug.WriteLine("Detection failed.");
                }
            }

如果是.net core 使用且需使用GB2312编码格式、GBK编码格式
的话,还需安装一个encoding的包,原文:https://www.cnblogs.com/mermaidLoft/p/6047423.html

1. 引入了System.Text.Encoding.CodePages.dll

2. 在启动的时候,注册EncodingProvider,执行代码如下:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

在Program.cs文件加入:

    static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // Register CodePagesEncodingProvider
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

相关文章

  • c# 读取文本文件编码格式最安全简单的方式

    用此库:c# 读取文本文件编码格式使用 ude, https://github.com/errepi/ude,可以...

  • Spark 学习笔记(三)-数据读存-JSON

    JSON是一种半结构化的数据格式,最简单的读取方式是将数据作为文本文件读取,然后使用JSON解析器来对RDD的值进...

  • 文本文件的编码格式

    文本文件的编码格式,都在3A服务器上进行测试 一、文本文件 文本文件存储的内容是基于 字符编码 的文件,常见的编码...

  • java学习笔记#4-File

    编码浅析 编码格式实例 文本文件就是字符序列,它可以是任意编码,中文机器上创建文本文件时,默认ansi编码。不同的...

  • node基本模块之fs

    异步方式 读取文本文件 读取二进制文件

  • Python初学者入门随笔 01 Python 语法学习

    1. 中文编码 Python 中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字,所以在读取...

  • 文件编码格式

    文件编码格式 从文件编码的方式来看,文件可分为ASCII码文件和二进制码文件两种。 ASCII文件也称为文本文件,...

  • C_language_renew09

    文件 文件分两类:文本文件、二进制文件 文本文件:是基于字符编码的文件常见的编码有ASCII。以ASCII格式存放...

  • python3读写文件的方式:

    一、读取文本格式的文件:open 读取内容的方式: 写文件方式: 例: 二、读取csv格式的文件: 例:

  • 1.中文编码

    Python中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字,所以在读取中文时会报错。 ...

网友评论

      本文标题:c# 读取文本文件编码格式最安全简单的方式

      本文链接:https://www.haomeiwen.com/subject/hysqhktx.html