Reputation: 83
I need to check a file is finished copying in C#. Is there any event like iscopycompleted ? I need to check without opening the file. Many of the samples shows by opening the file.
When do the attributes of the file is created? What is the attribute of the file while it under copying? Is there any way to check the file is copying by using file attributes?
I have checked using the following code.
FileAttribute atr=File.getAttribute("FilePath");
I got the fileattribute as Archive while copying and after copy completed.
Upvotes: 5
Views: 12142
Reputation: 3326
you can compare the size of two files
long length1 = new System.IO.FileInfo("fromFile").Length;
//code for moving file here
long length2;
do{
length2 = new System.IO.FileInfo("toFile").Length;
} while (length1!=length2);
Upvotes: 1
Reputation: 343
This function keep working until the file is finished copying.
private bool CheckFileHasCopied(string FilePath)
{
try
{
if (File.Exists(FilePath))
using (File.OpenRead(FilePath))
{
return true;
}
else
return false;
}
catch (Exception)
{
Thread.Sleep(100);
return CheckFileHasCopied(FilePath);
}
}
Upvotes: 1
Reputation: 4372
Here is what you are looking for, Just control FileSystemWatcher Created Event:
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
bool flag = true;
while(flag)
{
try
{
File.Move(e.FullPath, @"C:\NewLocation\" + e.Name);
flag = false;
}
catch { }
}
}
Upvotes: 0
Reputation: 417
If you are trying to check whether file has been moved from one location to other location, then you can do something like this.
//Store the location of your destination folder here with FileName
string Destination = "Destination_Location/"+FileName;
//Now get the entire Destination Address with FileName using Server.MapPath
string FullPath = Server.MapPath(Destination );
//Now you can use File.Exists condition to check whether file is present in your Destination location or not
if (File.Exists(FullPath))
{
//File copy is completed
}
else
{
//File not present
}
Upvotes: 0