C# 如何在c中读取文件夹中的文本文件#

C# 如何在c中读取文件夹中的文本文件#,c#,C#,我在主文件夹示例中有5个不同的文本文件 c:\Main 在Main中,我有5个文件(test1.txt、test2.txt.test3.txt、test4.txt和test5.txt) 如何读取最新文件?我认为您应该使用“\\”如“c:\\Main”或使用此文件->@找不到源类型“string[]”的查询模式的实现找不到OrderByDescending。您是否缺少对“System.Core.dll”的引用或对“System.Linq”的using指令?请尝试添加对System.Core的引用以

我在主文件夹示例中有5个不同的文本文件

c:\Main

在Main中,我有5个文件(test1.txt、test2.txt.test3.txt、test4.txt和test5.txt)


如何读取最新文件?

我认为您应该使用“\\”如“c:\\Main”或使用此文件->@找不到源类型“string[]”的查询模式的实现找不到OrderByDescending。您是否缺少对“System.Core.dll”的引用或对“System.Linq”的using指令?请尝试添加对System.Core的引用以及对System.Linq和System.Data.Linq的using指令。
using System.Linq;

//Get LatestFile  
var directory = new DirectoryInfo("c:\\Main");
var LatestFile = (from f in directory.GetFiles()
             orderby f.LastWriteTime descending
             select f).First();
//Read contents 
 string contents = File.ReadAllText(LatestFile);