问题
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 :
- You start moving the file
- Monitoring system kick in and start uploading to CDN
- File was parialy uploaded
- 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