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:\temp\ascii.txt");

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

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

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

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM