C # scan a folder and open files created after a certain time

I am writing a small program in C # that scans a folder and opens files created after 5:30 pm after clicking a button in the program. It will also have to be looked for in subfolders.

I need several solutions to point me in the right direction, as I am not sure how to do this.

This is part of the folder viewer. The problem is when the user goes home, the PC is turned off, and the files are created in the directory after 17.30. Therefore, I need a way, when the program restarts in the morning, it detects something created after 17.30 and opens them.

private void button1_Click(object sender, EventArgs e) { folderBrowser.ShowDialog(); textBox1.Text = folderBrowser.SelectedPath; filewatcher.Path = textBox1.Text; Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\COMPANY\\FOLDERWATCHER", "FOLDERPATH", textBox1.Text); } private void Form1_Load(object sender, EventArgs e) { String WatchFolder = Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\COMPANY\\FOLDERWATCHER", "FOLDERPATH", "").ToString(); textBox1.Text = WatchFolder; filewatcher.Path = WatchFolder; } private void Form1_Resize(object sender, EventArgs e) { if (WindowState == FormWindowState.Minimized) { ShowInTaskbar = true; Hide(); } } private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e) { if(!e.FullPath.EndsWith("temp.temp")) { MessageBox.Show("You have a Collection Form: " + e.Name); Process.Start("explorer.exe", e.FullPath); } } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { Show(); } } 

This is my complete code above. I would like to use the button to open or show files created after 17.30.

+6
source share
4 answers

Look at the System.IO namespace, it has everything you need.

the DirectoryInfo and File classes will do what you want.

+19
source

Here is the recursive method you are looking for:

 public static List<string> GetFilesCreatedAfter(string directoryName, DateTime dt) { var directory = new DirectoryInfo(directoryName); if (!directory.Exists) throw new InvalidOperationException("Directory does not exist : " + directoryName); var files = new List<string>(); files.AddRange(directory.GetFiles().Where(n => n.CreationTime > dt).Select(n=>n.FullName)); foreach (var subDirectory in Directory.GetDirectories(directoryName)) { files.AddRange(GetFilesCreatedAfter(subDirectory,dt)); } return files; } 

Hope I helped.

+5
source

You can use FileSystemWatcher ( MSDN documentation ) to detect files that were created after clicking a button (while your application is running).

 FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = "C:\\YourDirectory"; watcher.Created += (sender, args) => { // File was created } watcher.EnableRaisingEvents = true; 

This allows you to track files as they are created (while your application is running).

If you just want to get a list of all the directories created for a certain period of time (before launching your application), you can search the directory tree using Directory.GetDirectories and Directory.GetFiles .

+3
source

Instead of date and time, specify the date and time.

 void DirSearch(string dir) { try { foreach (string d in Directory.GetDirectories(dir)) { foreach (string f in Directory.GetFiles(d, "*.*")) { if(DateTime.Compare(f.GetCreationTime, datetime)) { //files found } } DirSearch(d); } } catch (System.Exception excpt) { Console.WriteLine(excpt.Message); } } 
+1
source

Source: https://habr.com/ru/post/891015/


All Articles