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

天涯浪子 提交于 2019-12-21 13:30:44

问题


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


来源:https://stackoverflow.com/questions/11255360/how-do-you-get-the-name-of-a-new-file-created-using-filesystemwatcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!