1
votes

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?

2
I need to figure out about the File.Move but it would be better if you upload the files to CDN only if any file is moved completely.. - PawanS
@GAPS We use CuteFTP to monitor the destination folder. So I could't program it to check whether a file is in use before moving the same to CDN :( - Gopi
You will see type of exception and you can give a try with console app to make it sure. - Nipun Ambastha
@NipunAmbastha he's actually using that. - aiapatag

2 Answers

1
votes

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);
        }
    }
1
votes

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