Before giving you the answer consider that moving files in the same drive doesn't transfer the data, only affect the directory entries in the hard drive, so it would be very fast,
Other simpler option you can implement is to determine if copying/moving the file based on the file size, that way you dont have to wait for the timeout.
Well here is the code for copying with timeout, this one is implemented using an async method with a timed cancellation token:
static async Task Copy(string destFilePath, string sourceFilePath, int timeoutSecs)
{
var cancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSecs));
using (var dest = File.Create(destFilePath))
using (var src = File.OpenRead(sourceFilePath))
{
await src.CopyToAsync(dest, 81920, cancellationSource.Token);
}
}
public async Task CopyAsync(string sourceFilePath, string destFilePath, int timeoutSecs)
{
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSecs));
using (var dest = File.Create(destFilePath))
using (var src = File.OpenRead(sourceFilePath))
{
await src.CopyToAsync(dest, 81920, cancellationTokenSource.Token);
}
}
Member
583 Points
265 Posts
File Move with in time limits
Jul 22, 2017 07:07 PM|jayakumarvinayagam|LINK
HI All,
I have a requirement like to move/copy a file from folder to another folder with in time limits.
Folder: Input
file Name: sample.txt
Folder: Output
the same sample.txt,
just assume that sample.txt has 500mb size, it takes 5 minutes to complete, but I wanna set time out for 3 minutes.
if it takes more than 3minutes, stop the moving process, like sample.txt file resides in same input folder.
Thanks,
Participant
1380 Points
608 Posts
Re: File Move with in time limits
Jul 23, 2017 04:04 AM|JBetancourt|LINK
Before giving you the answer consider that moving files in the same drive doesn't transfer the data, only affect the directory entries in the hard drive, so it would be very fast,
Other simpler option you can implement is to determine if copying/moving the file based on the file size, that way you dont have to wait for the timeout.
Well here is the code for copying with timeout, this one is implemented using an async method with a timed cancellation token:
then you can do:
thanks
Please remember to click "Mark as Answer" the responsES that resolved your issue.
Member
583 Points
265 Posts
Re: File Move with in time limits
Jul 25, 2017 05:49 AM|jayakumarvinayagam|LINK
Thanks, I updated little bit in order to accomplish my task.
I will remove the file in the source if it successfully copied from source to destination.
If it canceled in between some buffered content in the destination folder, so I will remove those file if it not fully copied from the source.
Thanks,