等待在C#中创建文件

时间:2021-01-11 20:55:49

Is there a built in method for waiting for a file to be created in c#? How about waiting for a file to be completely written?

是否有内置方法等待在c#中创建文件?如何等待文件完全写入?

I've baked my own by repeatedly attempting File.OpenRead() on a file until it succeeds (and failing on a timeout), but spinning on a file doesn't seem like the right thing to do. I'm guessing there's a baked-in method in .NET to do this, but I can't find it.

我通过在文件上反复尝试File.OpenRead()直到它成功(并且在超时时失败)来烘焙自己,但是在文件上旋转似乎不是正确的事情。我猜测在.NET中有一个烘焙方法可以做到这一点,但我找不到它。

4 个解决方案

#1


What about using the FileSystemWatcher component ?

使用FileSystemWatcher组件怎么样?

This class 'watches' a given directory or file, and can raise events when something (you can define what) has happened.

这个类'监视'给定的目录或文件,并且可以在发生某些事情(您可以定义什么)时引发事件。

#2


When creating a file with File.Create you can just call the Close Function. Like this:

使用File.Create创建文件时,您只需调用Close函数即可。像这样:

File.Create(savePath).Close();

#3


FileSystemWatcher can notify you when a file is created, deleted, updated, attributes changed etc. It will solve your first issue of waitign for it to be created.

FileSystemWatcher可以在创建,删除,更新,更改属性等文件时通知您。它将解决您创建的第一个waitign问题。

As for waiting for it to be written, when a file is created, you can spin off and start tracking it's size and wait for it stop being updated, then add in a settle time period, You can also try and get an exclusive lock but be careful of locking the file if the other process is also trying to lock it...you could cause unexpected thigns to occur.

至于等待它被写入,当创建文件时,你可以旋转并开始跟踪它的大小并等待它停止更新,然后添加一个结算时间段,你也可以尝试获得一个独占锁定但是如果其他进程也试图锁定文件,请小心锁定文件...您可能会导致意外的发生。

#4


FileSysWatcher cannot monitor network paths. In such instances, you manually have to "crawl" the files in a directory -- which can result in the above users error.

FileSysWatcher无法监控网络路径。在这种情况下,您手动必须“爬行”目录中的文件 - 这可能导致上述用户错误。

Is there an alternative, so that we can be sure we don't open a file before it has been fully written to disk and closed?

是否有替代方案,以便我们可以确保在完全写入磁盘并关闭之前不打开文件?

#1


What about using the FileSystemWatcher component ?

使用FileSystemWatcher组件怎么样?

This class 'watches' a given directory or file, and can raise events when something (you can define what) has happened.

这个类'监视'给定的目录或文件,并且可以在发生某些事情(您可以定义什么)时引发事件。

#2


When creating a file with File.Create you can just call the Close Function. Like this:

使用File.Create创建文件时,您只需调用Close函数即可。像这样:

File.Create(savePath).Close();

#3


FileSystemWatcher can notify you when a file is created, deleted, updated, attributes changed etc. It will solve your first issue of waitign for it to be created.

FileSystemWatcher可以在创建,删除,更新,更改属性等文件时通知您。它将解决您创建的第一个waitign问题。

As for waiting for it to be written, when a file is created, you can spin off and start tracking it's size and wait for it stop being updated, then add in a settle time period, You can also try and get an exclusive lock but be careful of locking the file if the other process is also trying to lock it...you could cause unexpected thigns to occur.

至于等待它被写入,当创建文件时,你可以旋转并开始跟踪它的大小并等待它停止更新,然后添加一个结算时间段,你也可以尝试获得一个独占锁定但是如果其他进程也试图锁定文件,请小心锁定文件...您可能会导致意外的发生。

#4


FileSysWatcher cannot monitor network paths. In such instances, you manually have to "crawl" the files in a directory -- which can result in the above users error.

FileSysWatcher无法监控网络路径。在这种情况下,您手动必须“爬行”目录中的文件 - 这可能导致上述用户错误。

Is there an alternative, so that we can be sure we don't open a file before it has been fully written to disk and closed?

是否有替代方案,以便我们可以确保在完全写入磁盘并关闭之前不打开文件?