1

I would like to know how to make something which will work like that. Since Java 1.7 I could use WatchService but how to do that in C#? I would like to make this method blocking, not a usual event, so it waits forever until new file will come up.

while(isANewFileInDirectory(path)){
     doSomeCode();
}

EDIT:

     FileSystemWatcher watcher = new FileSystemWatcher(".\\Screenshots");
                    watcher.Created += watcher_Created;

   void watcher_Created(object sender, FileSystemEventArgs e) {
            System.Diagnostics.Debug.WriteLine("fasuigfasuigf423432FSDFSAasuigf");
            sendResponse(e.FullPath);
        }
4

2 回答 2

3

一定要阻塞吗?您可以使用 Filewatcher... http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

编辑:这是一个例子:

namespace FileWatchTest
{
    class Program
    {
        static void Main(string[] args)
        {
            FileSystemWatcher watcher = new FileSystemWatcher(@"c:\temp");
            watcher.EnableRaisingEvents = true;
            watcher.Created += (obj, arg) => Console.WriteLine("File {0} created", arg.Name);
            watcher.Deleted += (obj, arg) => Console.WriteLine("File {0} deleted", arg.Name);
            watcher.Changed += (obj, arg) => Console.WriteLine("File {0} changed", arg.Name);

            Console.ReadLine();
        }
    }
}

将其粘贴到控制台应用程序中,您应该会看到更新(显然 - 更改您要观看的文件夹的路径...)

于 2014-02-07T09:39:43.667 回答
1

寻找 FileSystemWatcher 类。MSDN 有详细信息和示例代码。这里

于 2014-02-07T09:40:44.043 回答