如何测试当前是否正在写入文件

wil*_*san 14 .net c#

我有一个应用程序必须检查文件夹并读取复制到其中的任何文件.如何测试当前是否正在写入该文件夹中的文件?我只想读取已写入所有数据并关闭的文件.

Kyl*_*ndo 17

它不干净,但它有效:

try
{
    using (Stream stream = new FileStream("File.txt"))
    {
    }
} 
catch 
{
    // Check if file is in use (or whatever else went wrong), and do any user communication that you need to
}
Run Code Online (Sandbox Code Playgroud)

我并不特别喜欢使用异常作为条件,但据我所知,这是关于最简单,最好(也是唯一可靠)的方法.