我只想从一个目录返回 10 个文件。这可能吗?
DirectoryInfo d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/xml"));
FileInfo[] files = d.GetFiles("*.xml");
这种方式返回所有 XML 文件,但我只想获取前十个。
最佳答案
如果您使用的是 .NET4,那么您应该使用 EnumerateFiles
相反,连同 Take
扩展方法:
var d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/xml"));
FileInfo[] files = d.EnumerateFiles("*.xml").Take(10).ToArray();
关于c# - DirectoryInfo GetFiles TOP 编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5899561/