文件是否正在被复制? [英] Is file being copied right now?

查看:131
本文介绍了文件是否正在被复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

$ b是一个文件存在和读取的时间$ b

我正在使用文件观察器来检查文件是否在目录中。如果是的话,那么我想打开它并移到另一个目录。

我的方法是使用 FileShare.None 以确保独占访问。我所做的是:

$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
{
FileSystemWatcher fileWatcher = new FileSystemWatcher();
fileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
fileWatcher.Created + = fileWatcher_Created;
fileWatcher.EnableRaisingEvents = true;
Console.ReadLine();


private static void fileWatcher_Created(object sender,FileSystemEventArgs e)
{
WorkOnFile(e.FullPath);
}


//必须完全完成。我如何确保它?
私有静态无效WorkOnFile(字符串文件名)
{
使用(FileStream f = File.Open(fileName,FileMode.Open,FileAccess.Read,FileShare.None))
{
Thread.Sleep(40000); //一些长操作



code $ $ $ $ $ $ $ $ $ $ $ $ $底线是,它必须完成 WorkOnFile()并且只有一次文件。如果一个文件被复制,那么我需要再次调用 WorkOnFile()直到它被完全复制,并且没有进程正在使用它。



我如何达到?如果你需要这个文件尽可能快地移动,你可以尝试使用FileWatcher来监视'LastWrite',并在一个小的延迟,试图移动文件。



我必须做一些非常相似的事情,但是不要着急,只是重要的一点是文件可靠地移动。所以我使用了一个System.Timers.Timer来运行一次:

  var scanDirectoryIn = new DirectoryInfo(folderIn); 
foreach(scanDirectoryIn.GetFiles()中的var fileInfo)
{
if(fileInfo.Extension!=.csv)continue;如果(DateTime.UtcNow.Subtract(fileInfo.LastWriteTimeUtc).TotalMinutes< 5)继续,则
;
尝试
{
fileInfo.MoveTo(folderOut +\\+ fileInfo.Name);

catch(Exception){}
}


Possible Duplicate:
C# - from time to time check if a file exists and read from it

I'm using file watcher to check if a file was craeted in a directory. If it was, then I want to open it and remove to another directory.

My approach is also to use FileShare.None to an ensure exclusive access. What I did is:

class Program
    {
        private static void Main(string[] args)
        {
            FileSystemWatcher fileWatcher = new FileSystemWatcher();
            fileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            fileWatcher.Created += fileWatcher_Created;
            fileWatcher.EnableRaisingEvents = true;
            Console.ReadLine();
        }

        private static void fileWatcher_Created(object sender, FileSystemEventArgs e)
        {
            WorkOnFile(e.FullPath);   
        }


        //must be done completely. How do I ensure it?
        private static void WorkOnFile(string fileName)
        {
            using (FileStream f = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                Thread.Sleep(40000); // some long operations
            }            
        }
  }

The bottom line is, that it necessary to do WorkOnFile() completely and only one time for one file. If a file being copied, then I need to call WorkOnFile() again until it has completely been copied and none process is using it.

How do I reach it?

解决方案

If you need that file moved as fast as possible, you could try using FileWatcher to monitor 'LastWrite' and after a small delay, trying to move the file.

I had to do something very similar, but there was no hurry, just important that the file was moved reliably. So I instead used a System.Timers.Timer to run once a minute:

var scanDirectoryIn = new DirectoryInfo(folderIn);
foreach (var fileInfo in scanDirectoryIn.GetFiles())
{
  if (fileInfo.Extension != ".csv") continue;
  if (DateTime.UtcNow.Subtract(fileInfo.LastWriteTimeUtc).TotalMinutes < 5) continue;
  try
  {
    fileInfo.MoveTo(folderOut + "\\" + fileInfo.Name);
  }
  catch (Exception) {}
}

这篇关于文件是否正在被复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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