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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
回答2:
private void watcher_OnCreated(object source, FileSystemEventArgs e)
{
String Filename = Path.GetFilename(e.FullPath);
}