File.Move locks file in destination folder?

安稳与你 提交于 2019-12-11 06:06:34

问题


I use File.Move to move large files around 2GB from a directory to another directory.The desitnation folder is monitored and if there is any new file, it will be uploaded to CDN. But we experienced some partial file upload to CDN which means, the respective file was uploaded to CDN while the same file was moving from source to destination directory. So, I need to know whether File.Move locks the file destination folder till the file is completed moved?


回答1:


What you can do to avoid partial upload to CDN is to hide it first when moving it and unhide it once it's completely done. And have the monitoring tool not transfer it to the CDN if the file is still hidden.

Or you can lock it out so that other processes (which is your monitoring tool -- CuteFTP) can't access the destination file until the stream is already finished.

e.g.

    static void Main(string[] args)
    {
        string sourcePath = "mytext.txt";
        string destPath = @"dest\mytext.txt";
        using (FileStream sourceStream = new FileStream(sourcePath, FileMode.Open))
        {
            using (FileStream destStream = new FileStream(destPath, FileMode.Create))
            {
                destStream.Lock(0, sourceStream.Length);
                sourceStream.CopyTo(destStream);
            }
        }

        if (File.Exists(sourcePath))
        {
            File.Delete(sourcePath);
        }
    }



回答2:


Your problem is the moonitoring on the destination folder.

Since you have a big file, it take time to copy it so what happend is :

  1. You start moving the file
  2. Monitoring system kick in and start uploading to CDN
  3. File was parialy uploaded
  4. You finish moving the file.

One mitigation for this is , assuming your monitoring system searching for files with some extensation - moving MyBigFile.ext to MyBigFile.ext.tmp. after finish , rename it back to MyBigFile.ext, so when monitoring kick in , it will have the complete file



来源:https://stackoverflow.com/questions/17087181/file-move-locks-file-in-destination-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!