c#中如何直接读到文本文件的最后一行

ghost_029 2009-05-06 04:41:52
c#中如何直接读到文本文件的最后一行
不用循环
...全文
255 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wrinky 2009-05-06
  • 打赏
  • 举报
回复

using System;
using System.IO;

class MyClass
{
public static void Main()
{
string[] textArr = File.ReadAllLines("error.txt");
if (textArr.Length > 0)
{
string result = textArr[textArr.Length - 1];
Console.WriteLine(result);
}
}
}


楼上的方法是对的,ReadAllLine()就是把文件按行存入string数组。
mdq001 2009-05-06
  • 打赏
  • 举报
回复


string[] textArr = File.ReadAllLines("d:\\error.txt");
if (textArr.Length > 0)
{
string result = textArr[textArr.Length - 1];
}


只读最后一行
mdq001 2009-05-06
  • 打赏
  • 举报
回复



string text = File.ReadAllText("d:\\error.txt");

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧