开发者

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

I am writing a little program in C# that scans a folder and opens the files that have been created after 5.30pm after a button has been pressed on the program. This will also have to search within sub-folders.

I need a couple of solutions to p开发者_如何学JAVAoint me in the correct direction as I'm not sure how I would do this.

This is part of a folder watcher program. The problem is when the user goes home the PC is switched off and there are files being created to the directory after 17.30. So I need a way for when the program is restarted in the morning it detects anything created after 17.30 and open 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 a button to open or show the files created after 17.30.


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

the DirectoryInfo and File classes will do what you want.


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.


You can use FileSystemWatcher (MSDN documentation) to detect files that have been created after pressing 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 directories that were created in a specified time range (before your application was started), then you can search the directory tree using Directory.GetDirectories and Directory.GetFiles.


In place of datetime place you date and time value.

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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜