C# 如何在某个目录中的应用程序实例期间获取新创建的文件列表?
我想知道如何在某个目录中的应用程序实例期间获取新创建的文件列表 当我的申请打开时 需要明确的是:每次打开我的应用程序时,在某个目录中生成的文件很少,我只需要打开应用程序后创建的文件的列表。使用FileWatcherC# 如何在某个目录中的应用程序实例期间获取新创建的文件列表?,c#,file,directory,filesystemwatcher,C#,File,Directory,Filesystemwatcher,我想知道如何在某个目录中的应用程序实例期间获取新创建的文件列表 当我的申请打开时 需要明确的是:每次打开我的应用程序时,在某个目录中生成的文件很少,我只需要打开应用程序后创建的文件的列表。使用FileWatcher FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = @"C:\temp"; watcher.NotifyFilter = NotifyFilters.FileName;
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\temp";
watcher.NotifyFilter = NotifyFilters.FileName;
watcher.Filter = "*.*";
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;
private static void OnCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File created {e.Name}");
}
使用FileWatcher
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\temp";
watcher.NotifyFilter = NotifyFilters.FileName;
watcher.Filter = "*.*";
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;
private static void OnCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File created {e.Name}");
}
存储应用程序打开时间打开时,请检查在此时间之后创建的文件。您需要在指定的时间之后修改文件。DateTime date=DateTime.Now.AddHours(-1);DirectoryInfo dirInfo=新的DirectoryInfo(@“c:\temp”);List filesInfo=dirInfo.GetFiles().Where(x=>x.LastWriteTime>=date.ToList();存储应用程序打开时间打开时,请检查在此时间之后创建的文件。您需要在指定的时间之后修改文件。DateTime date=DateTime.Now.AddHours(-1);DirectoryInfo dirInfo=新的DirectoryInfo(@“c:\temp”);List filesInfo=dirInfo.GetFiles().Where(x=>x.LastWriteTime>=date.ToList();