Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# file IO
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 16-Oct-12 23:56pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Something like this you can try.
 
 
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 [^]
  Permalink  
v2
Comments
Pasan Indeewara at 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 at 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)

  Print Answers RSS
0 OriginalGriff 675
1 CPallini 320
2 Sergey Alexandrovich Kryukov 305
3 BillWoodruff 285
4 Peter Leow 255


Advertise | Privacy | Mobile
Web01 | 2.8.141029.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100