/// <summary>
        /// txt に書き足す。List<string>を記録する
        /// </summary>
        /// <param name="textFileName"></param>
        /// <param name="writtenText"></param>
        /// <returns></returns>
        public static void WriteText(string textFileName, List <string> writtenText, bool append, string encodingName = "Shift_JIS")
        {
            string path = IOText.MakeFullPath(textFileName);

            //NugetでSystem.Text.Encoding.CodePagesをダウンロードしておく.
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            try
            {
                using (var writer = new StreamWriter(path, append, Encoding.GetEncoding(encodingName)))
                {
                    foreach (string w in writtenText)
                    {
                        writer.WriteLine(w);
                    }
                }
            }
            catch
            {
                using (var writer = new StreamWriter(path, false, Encoding.GetEncoding(encodingName)))
                {
                    foreach (string w in writtenText)
                    {
                        writer.WriteLine(w);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// txtデータを読み込む。string[]を読み込む。
        /// </summary>
        /// <param name="textFileName"></param>
        /// <returns></returns>
        public static string[] ReadText(string textFileName, string encodingName = "Shift_JIS")
        {
            string path = IOText.MakeFullPath(textFileName);

            //.Net Coreでtxtを使用するのに必要。
            //NugetでSystem.Text.Encoding.CodePagesをダウンロードしておく.
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            List <string> temp = new List <string>();

            if (File.Exists(path))
            {
                using (var reader = new StreamReader(path, Encoding.GetEncoding(encodingName)))
                {
                    while (!reader.EndOfStream)
                    {
                        temp.Add(reader.ReadLine());
                    }
                    reader.Dispose();
                }
            }
            else
            {
                throw new FormatException("404 File Not Found : " + nameof(textFileName));
            }

            int blank = 0;

            for (int i = 0; i < temp.Count; i++)
            {
                if (temp[temp.Count - 1 - i] != "")
                {
                    break;
                }
                blank++;
            }
            for (int i = 0; i < blank; i++)
            {
                temp.RemoveAt(temp.Count - 1);
            }
            return(temp.ToArray());
        }