C# winfrom 读取txt文本内容

第一种:

        /// <summary>
        /// 读取txt文件内容
        /// </summary>
        /// <param name="Path">文件地址</param>
        public void ReadTxtContent(string Path)
        {
            StreamReader sr = new StreamReader(Path, Encoding.Default);
            string content;
            while ((content = sr.ReadLine()) != null)
            {
                Console.WriteLine(content.ToString());
            }
        }

第二种:

它们都一次将文本内容全部读完,并返回一个包含全部文本内容的字符串
string str = File.ReadAllText(@"c: empascii.txt");

// 也可以指定编码方式 
string str2 = File.ReadAllText(@"c: empascii.txt", Encoding.ASCII);

也可以使用方法File.ReadAllLines。该方法返回一个字符串数组。每一行都是一个数组元素。

string[] strs = File.ReadAllLines(@"c: empascii.txt"); 

// 也可以指定编码方式 
string[] strs2 = File.ReadAllLines(@"c: empascii.txt", Encoding.ASCII);

原文地址:https://www.cnblogs.com/yinmu/p/10994947.html