开发者

C# and SystemFileWatcher- multiple files

I am using the FileSystemWatcher to watch a folder for new files. When a new file is copied into it, it worked well for me. However, if I copy in 5 files (That would be the max I would ever do at once), it fires, but the FileSystemEventArgs only has one file.

I need it to pass all the new files.

Is there a way to make it handle all the files, and then I loop through them?

Here is my code:

static void Main(string[] args)
{
    FileSystemWatcher fsw = new FileSystemWatcher(FolderToMonitor)
                                {
                                    InternalBufferSize = 10000
                                };
    fsw.Created += new FileSystemEventHandler(fsw_Created);
    bool monitor = true;

    Show("Waiting...", ConsoleColor.Green);
    while (monitor)
    {
        fsw.WaitForChanged(WatcherChangeTypes.All, 2000); // Abort after 2 seconds to see if there has been a user keypress.
        if (Console.KeyAvailable)
        {
            monitor = false;
        }
    }

    Show("User h开发者_JS百科as quit the process...", ConsoleColor.Yellow);
    Console.ReadKey();
}`        

static void fsw_Created(object sender, FileSystemEventArgs args)
{
    Show("New File Detected!", ConsoleColor.Green);
    Show("New file name: " + args.Name, ConsoleColor.Green);

    bool fileIsReadOnly = true;

    while (fileIsReadOnly)
    {
        Thread.Sleep(5000);
        fileIsReadOnly = IsFileReadonly(args.FullPath);

        if (fileIsReadOnly)
            Show("File is readonly... waiting for it to free up...", ConsoleColor.Yellow);
    }
    Show("File is not readonly... Continuing..", ConsoleColor.Yellow);

    HandleFile(args);
}


If I remember correctly, the watcher fires multiple events, one for each file.

Also notice this:

The Windows operating system notifies your component of file changes in a buffer created by the FileSystemWatcher. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification. Increasing the size of the buffer with the InternalBufferSize property is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small yet large enough to not miss any file change events. To avoid a buffer overflow, use the NotifyFilter and IncludeSubdirectories properties so you can filter out unwanted change notifications.

source: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx


You need to do two changes:

  1. Increase the buffer size.
  2. EnableRaisingEvents.

See the modified code below:

static void Main(string[] args)
{
FileSystemWatcher fsw = new FileSystemWatcher(FolderToMonitor)
                            {
                                InternalBufferSize = 65536
                            };
fsw.EnableRaisingEvents = true;
fsw.Created += new FileSystemEventHandler(fsw_Created);
bool monitor = true;

Show("Waiting...", ConsoleColor.Green);
while (monitor)
{
    fsw.WaitForChanged(WatcherChangeTypes.All, 2000); // Abort after 2 seconds to see if there has been a user keypress.
    if (Console.KeyAvailable)
    {
        monitor = false;
    }
}
Show("User has quit the process...", ConsoleColor.Yellow);
Console.ReadKey();

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜