How do you know if a file is still be written to?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • napstar
    New Member
    • Nov 2006
    • 55

    How do you know if a file is still be written to?

    I'm copying files generated by an application periodically to my PC from a network location,but sometimes the files are still be written when I copy them.
    How can I tell if the file is still be written to?

    public void copyFile(string source, string destination)
    {
    DirectoryInfo dir = new DirectoryInfo(s ource);
    FileInfo[] files = dir.GetFiles();
    foreach (FileInfo f in files)
    {
    DateTime currentTime = DateTime.Now;

    DateTime tolerance = currentTime.Sub tract(new TimeSpan(0, 5, 0));
    if (tolerance.Comp areTo(f.LastWri teTime) > 0)
    {


    f.CopyTo(destin ation + "" + f.Name, true);


    }
    }//end of foreach
    }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I posted a potential solution to this in another thread but I don't think the OP ever got back if it worked or not. I think it will.

    Try to open the file with READ+WRITE access and be exclusive, say no sharring allowed. I *think* it will throw an exception if the file is still being "written" to when you try that, so that is one way to tell.

    Comment

    Working...