Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the name of a new file created using FileSystemWatcher?

I'm monitoring a folder using FileSystemWatcher. If I download a file into there, how do I get the name of that downloaded file? For example, if I downloaded a file named TextFile.txt, how would I have it return that as a string? I am assuming this will work for all four triggers (changed, created, deleted, renamed)? I have IncludeSubdirectories set to true, so it should be able to do that.

like image 548
Minicl55 Avatar asked Jun 29 '12 02:06

Minicl55


People also ask

What is the use of file watcher?

File Watcher is an IntelliJ IDEA tool that allows you to automatically run a command-line tool like compilers, formatters, or linters when you change or save a file in the IDE.

Which of the following are types of changes that can be detected by the FileSystemWatcher?

The FileSystemWatcher provides us with the event handlers to capture events like renamed, deleted, created and changed.

What is the parameter that has to be used when a file is renamed after setting a FileSystemWatcher on the file?

For example, to watch for renaming of text files, set the Filter property to "*. txt" and call the WaitForChanged method with a Renamed specified for its parameter. The Windows operating system notifies your component of file changes in a buffer created by the FileSystemWatcher.


2 Answers

On the OnCreated event, add this code:

private void watcher_OnCreated(object source, FileSystemEventArgs e)
{
    FileInfo file = new FileInfo(e.FullPath);
    Console.WriteLine(file.Name); // this is what you're looking for.
}

See FileInfo Class @ MSDN

like image 177
John Woo Avatar answered Nov 01 '22 22:11

John Woo


private void watcher_OnCreated(object source, FileSystemEventArgs e)
{
    String Filename = Path.GetFilename(e.FullPath);
}
like image 3
ThunderGr Avatar answered Nov 01 '22 21:11

ThunderGr