Click here to Skip to main content
13,055,408 members (53,706 online)
Rate this:
Please Sign up or sign in to vote.
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 16-Oct-12 23:56pm

1 solution

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

Solution 1

Something like this you can 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 [^]
Pasan Indeewara 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2017
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