如何知道文本文件的写入是否完成或文件是否正在由另一个进程写入? [英] How to know whether a Text file write is complete or file is being writen by another process?

查看:85
本文介绍了如何知道文本文件的写入是否完成或文件是否正在由另一个进程写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的C#程序中正在复制文件.目前,当文件在某个外部第3方软件写入的共享位置上可用时,它只是在复制.

当我们的程序复制此文本文件时,它可能未完全写入磁盘,在这种情况下,我们的应用程序正在读取部分写入的文本文件.有没有办法知道文件是否已完全写入磁盘.

当前,我们的工作是等待IO进程线程等待几分钟(如果刚刚创建该文件),以确保完全复制该文件.但是,如果网络速度较慢,则可能会延迟并出现问题.

There is a file copying is being done in our C# program. At the moment it''s just copying when the file is available on a shared location which is written by some external 3rd party s/w.

When our program is copying this text file it may not be fully written to the disk, in that case partially written text file is being read by our App. Is there a way to know whether file is completely written to disk.

Currently what we do is we wait our IO process thread to wait for couple of mins if the file is just created in order to ensure it''s fully copied. But in case of slow network, it might get delayed and the problem would come up.

Any idea on this, a flag/file property to check this?

推荐答案

类似的东西,您可以尝试.

Something like this you can try.

try
{
 using (FileStream fs = File.Open("FileName", FileMode.Open, FileAccess.ReadWrite,  FileShare.None))
  {
    //File copy                 
  }
}
catch (Exception)
{
   // File is in use. 
}



或者,您可以使用 FileSystemWatcher [



Or you can use this FileSystemWatcher [^]


这篇关于如何知道文本文件的写入是否完成或文件是否正在由另一个进程写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆