Filewatcher创建活动创建活动、Filewatcher

2023-09-03 21:29:02 作者:坐等头发干

我监视的文件夹与.NET filewatcher对某些类型的文件(*。mbxml)。我使用filewatcher它的创建的事件。一旦创建了事件触发我有这个文件移动到另一个文件夹。这种方法的问题是,所创建的事件时立即发射作为文件复印开始。因此,如果该文件的时间太长复制到该文件夹​​被监视,在code表示移动文件失败。我搜索,我发现在网络上唯一的解决办法是,将文件移动一个try-catch块内,并不断尝试,直到整个文件被复制。我不喜欢这种解决方案,它会一直更好,如果创建的事件被解雇,一旦整个文件已经完成复制或有它一个单独的事件。是否有实现这一目标的另一种方式?

I am monitoring a folder with a .net filewatcher for certain kind of files(*.mbxml). I am using the created event of filewatcher for it. Once the created event fires I have to move this file to another folder. The problem with this approach is that the created event is fired as soon as the file copying starts. So if the file is taking too long to copy to the folder being watched, the code that moves the file fails. I've searched and the only solution I found on the net was that you move the file within a try-catch block and keep trying until the whole file is copied. I don't like this solution, it would've been better if the created event was fired once the whole file had finished copying or there was a separate event for it. Is there another way of achieving this?

推荐答案

我还没有看到这样做,无论如何,除非用try catch块。通常在OnCreate事件我设置了尝试的打开该文件一个布尔值。这就决定了如果code收益的休息,如果有另一种方式,我有兴趣为好。

i have yet to see this done in anyway except with a try catch block. usually on the oncreate event i set a bool that try's to open the file. this determines if the rest of the code proceeds, if there is another way i would be interested as well.

    private static bool creationComplete(string fileName)
    {
        // if the file can be opened it is no longer locked and now available
        try
        {
            using (FileStream inputStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                return true;
            }
        }
        catch (IOException)
        {
            return false;
        }
    }