C# 怎么在一个文件夹中查到日期最新的文件
展开全部
用linq最简单
using System.IO;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo info = new DirectoryInfo(@"C:\");
FileInfo newestFile = info.GetFiles().OrderBy(n => n.LastWriteTime).First();
}
}
}
不用linq
using System.IO;
using System.Linq;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo info = new DirectoryInfo(@"C:\");
FileInfo[] files = info.GetFiles();
DateTime time = new DateTime(1900, 1, 1);
FileInfo newestFile = null;
foreach (FileInfo f in files)
{
if (f.LastWriteTime > time)
{
time = f.LastWriteTime;
newestFile = f;
}
}
}
}
}
using System.IO;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo info = new DirectoryInfo(@"C:\");
FileInfo newestFile = info.GetFiles().OrderBy(n => n.LastWriteTime).First();
}
}
}
不用linq
using System.IO;
using System.Linq;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo info = new DirectoryInfo(@"C:\");
FileInfo[] files = info.GetFiles();
DateTime time = new DateTime(1900, 1, 1);
FileInfo newestFile = null;
foreach (FileInfo f in files)
{
if (f.LastWriteTime > time)
{
time = f.LastWriteTime;
newestFile = f;
}
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询