Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

file

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 point 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.

like image 574
Matt Avatar asked Jun 21 '11 14:06

Matt


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

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

the DirectoryInfo and File classes will do what you want.

like image 152
Neil N Avatar answered Sep 21 '22 07:09

Neil N


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.

like image 32
Maziar Taheri Avatar answered Sep 21 '22 07:09

Maziar Taheri