Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is a file copying is being done in our C# program. At the moment it's just copying when the file is available on a shared location which is written by some external 3rd party s/w.

When our program is copying this text file it may not be fully written to the disk, in that case partially written text file is being read by our App. Is there a way to know whether file is completely written to disk.

Currently what we do is we wait our IO process thread to wait for couple of mins if the file is just created in order to ensure it's fully copied. But in case of slow network, it might get delayed and the problem would come up.

Any idea on this, a flag/file property to check this?
Posted

1 solution

Something like this you can try.

C#
try
{
 using (FileStream fs = File.Open("FileName", FileMode.Open, FileAccess.ReadWrite,  FileShare.None))
  {
    //File copy                 
  }
}
catch (Exception)
{
   // File is in use. 
}


Or you can use this FileSystemWatcher [^]
 
Share this answer
 
v2
Comments
Pasan Eeriyagama 17-Oct-12 6:12am    
thx, bt appreciate if there's a method we can use without exclusive Open because the other process might fail in such case. Isnt' it.
Sushil Mate 17-Oct-12 6:16am    
it won't :) i guess you are single application is copying that file..
you can use filesystemwatcher class..
try first approach, if it is not suitable then go for filesystemwatcher.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900