コード例 #1
0
        /// <summary>
        /// Hnc8様のReadJEncを使用して文字コードの判別をする。
        /// </summary>
        public static Encoding JudgeFileEncoding(string path)         //2020.05.08 Mr-Ojii Hnc8様のReadJEncを使用して文字コードの判別をする。
        {
            if (!File.Exists(path))
            {
                return(null);
            }
            Encoding enc;
            FileInfo file = new FileInfo(path);

            using (Hnx8.ReadJEnc.FileReader reader = new Hnx8.ReadJEnc.FileReader(file))
            {
                // 判別読み出し実行。判別結果はReadメソッドの戻り値で把握できます
                Hnx8.ReadJEnc.CharCode c = reader.Read(file);
                // 戻り値のNameプロパティから文字コード名を取得できます
                string name = c.Name;
                Console.WriteLine("【" + name + "】" + file.Name);
                // GetEncoding()を呼び出すと、エンコーディングを取得できます
                enc = c.GetEncoding();
            }
            Debug.Print(path + " Encoding=" + enc.CodePage);

            if (enc == null)
            {
                enc = Encoding.GetEncoding(932);
            }
            return(enc);
        }
コード例 #2
0
        /// <summary>
        /// ファイルを開いて全文を取得する。
        /// </summary>
        /// <param name="strFilePath">開くファイルのパス</param>
        /// <param name="strReturn">ファイルの内容を返す</param>
        /// <returns>ファイルを開いて内容を取得できたか否か</returns>
        public bool loadFileAll(string strFilePath, out string strReturn)
        {
            strReturn = null;
            try
            {
                //Fileがあるかチェック。
                FileInfo fInfo = new FileInfo(strFilePath);
                if (!fInfo.Exists)
                {
                    return(false);
                }

                using (Hnx8.ReadJEnc.FileReader reader = new Hnx8.ReadJEnc.FileReader(fInfo))
                {
                    // 判別読み出し実行。判別結果はReadメソッドの戻り値で把握できます
                    Hnx8.ReadJEnc.CharCode c = reader.Read(fInfo);
                    // 戻り値のNameプロパティから文字コード名を取得できます
                    string name = c.Name;
                    // GetEncoding()を呼び出すと、エンコーディングを取得できます
                    System.Text.Encoding enc = c.GetEncoding();
                    // 実際に読み出したテキストは、Textプロパティから取得できます
                    // ※非テキストファイルの場合は、nullが設定されます
                    strReturn = reader.Text;
                }
            }
            catch (ArgumentException e)
            {
                MessageBox.Show("LoadFileAll Error:01\n" + e.ToString());
                return(false);
            }
            return(true);
        }
コード例 #3
0
        static (Encoding encoding, string newLine) DetectEncoding(string path)
        {
            var file = new FileInfo(path);

            using (var reader = new Hnx8.ReadJEnc.FileReader(file))
            {
                var charCode = reader.Read(file);
                if (reader.Text == null)
                {
                    return(null, null);
                }
                var encoding = charCode.GetEncoding();
                var newLine  = (reader.Text.DetectNewLineKind() ?? NewLineKind.CRLF).ToNewLineString();
                return(encoding, newLine);
            }
        }
コード例 #4
0
        /// <summary>
        /// Hnc8様のReadJEncを使用してテキストファイルを読み込む。
        /// 改行文字は、勝手に\nに統一する
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadTextFile(string path)
        {
            if (!File.Exists(path))
            {
                return(null);
            }
            string   str  = null;
            FileInfo file = new FileInfo(path);

            using (Hnx8.ReadJEnc.FileReader reader = new Hnx8.ReadJEnc.FileReader(file))
            {
                reader.Read(file);
                str = reader.Text;
            }

            str = str.Replace(JudgeNewLine(str), "\n");

            return(str);
        }
コード例 #5
0
        /// <summary>
        /// 文字コードを自動判別して複数のテキストファイルを読み取る。
        /// </summary>
        /// <param name="fileInfos">ファイル情報列挙。</param>
        /// <returns>
        /// 読み取った文字列のリスト。
        /// 読み取れなかったファイルについては null が格納される。
        /// </returns>
        public static List <string> ReadAll(IEnumerable <FileInfo> fileInfos)
        {
            if (fileInfos == null)
            {
                throw new ArgumentNullException(nameof(fileInfos));
            }
            if (!fileInfos.Any())
            {
                throw new ArgumentException(
                          @"`" + nameof(fileInfos) + @"` is empty.",
                          nameof(fileInfos));
            }
            if (fileInfos.Any(info => info == null))
            {
                throw new ArgumentException(
                          @"Some FileInfo in `" + nameof(fileInfos) + @"` are null.",
                          nameof(fileInfos));
            }

            var maxLength = fileInfos.Max(info => info.Length);

            if (maxLength > int.MaxValue)
            {
                throw new ArgumentException(@"Too large file.");
            }

            using (var reader = new Hnx8.ReadJEnc.FileReader((int)maxLength))
            {
                return
                    (fileInfos
                     .Select(
                         info =>
                {
                    reader.Read(info);
                    return reader.Text;
                })
                     .ToList());
            }
        }
コード例 #6
0
        /// <summary>
        /// 文字コードを自動判別して複数のテキストファイルを読み取る。
        /// </summary>
        /// <param name="fileInfos">ファイル情報列挙。</param>
        /// <returns>
        /// 読み取った文字列のリスト。
        /// 読み取れなかったファイルについては null が格納される。
        /// </returns>
        public static List<string> ReadAll(IEnumerable<FileInfo> fileInfos)
        {
            if (fileInfos == null)
            {
                throw new ArgumentNullException(nameof(fileInfos));
            }
            if (!fileInfos.Any())
            {
                throw new ArgumentException(
                    @"`" + nameof(fileInfos) + @"` is empty.",
                    nameof(fileInfos));
            }
            if (fileInfos.Any(info => info == null))
            {
                throw new ArgumentException(
                    @"Some FileInfo in `" + nameof(fileInfos) + @"` are null.",
                    nameof(fileInfos));
            }

            var maxLength = fileInfos.Max(info => info.Length);
            if (maxLength > int.MaxValue)
            {
                throw new ArgumentException(@"Too large file.");
            }

            using (var reader = new Hnx8.ReadJEnc.FileReader((int)maxLength))
            {
                return
                    fileInfos
                        .Select(
                            info =>
                            {
                                reader.Read(info);
                                return reader.Text;
                            })
                        .ToList();
            }
        }