How to tell if a file is currently being written to...

R

Rogelio

hey, got a method here (that I took from somewhere) that gets a list of all
the files in a folder.,

/// <summary>
/// Method that gets all the files in a directory and returns an
arraylist of all the files.
/// </summary>
/// <param name="di">Directory Info Object</param>
/// <param name="searchPattern">Search Pattern (*.pdf)</param>
/// <param name="MyFiles">referenced array list</param>
private void GetFiles(DirectoryInfo di, string searchPattern, ref
ArrayList MyFiles)
{
foreach (FileInfo fi in di.GetFiles(searchPattern))
{
MyFiles.Add(fi.FullName);
}
// comment out the following to not Search in subdirctories
foreach (DirectoryInfo d in di.GetDirectories())
{
GetFiles(d, searchPattern, ref MyFiles);
}

}



the problem is sometimes people are going to be pasting massive ifles in
this folder., like 100+ MB of size. and as you know it will put the file
there, but it wont be done yet.

what kind of "if" statement do I need that checks the file's status that its
not currently being written to?

thanks,
 
J

Jeroen Mostert

Rogelio said:
what kind of "if" statement do I need that checks the file's status that its
not currently being written to?
Unfortunately, there's no foolproof way of determining when a process is
"done" writing to a file. The best you can hope for is that the process
keeps open a write lock while it's writing to the file. Even then it mustn't
write in chunks (opening the file, appending some data, then closing it again).

If it does keep open a write lock, all you have to do is try to open the
file for writing with a lock of your own (new FileStream(path,
FileMode.Open, FileAccess.Write, FileShare.None)). If this fails, the file
is most likely still in use.

Another heuristic is to only process files whose last modified time
(FileInfo.LastWriteTime) is past some specified threshold, so you're
reasonably sure no more writes will occur.
 
R

Rogelio

ok. heres my solution....


/// <summary>
/// Method that gets all the files in a directory and returns an
arraylist of all the files.
/// </summary>
/// <param name="di">Directory Info Object</param>
/// <param name="searchPattern">Search Pattern (*.pdf)</param>
/// <param name="MyFiles">referenced array list</param>
private void GetFiles(DirectoryInfo di, string searchPattern, ref
ArrayList MyFiles)
{
foreach (FileInfo fi in di.GetFiles(searchPattern))
{
//check if the last write time of the file was not 20
seconds ago or sooner.
//this prevents a file currently being written to to be
added to the list of files.
if (fi.LastWriteTime < DateTime.Now.AddSeconds(-20))
{
MyFiles.Add(fi.FullName);
}
}
// Search in subdirctories
foreach (DirectoryInfo d in di.GetDirectories())
{
GetFiles(d, searchPattern, ref MyFiles);
}

}
 
P

Pavel Minaev

ok. heres my solution....

        /// <summary>
        /// Method that gets all the files in a directory and returns an
arraylist of all the files.
        /// </summary>
        /// <param name="di">Directory Info Object</param>
        /// <param name="searchPattern">Search Pattern (*.pdf)</param>
        /// <param name="MyFiles">referenced array list</param>
        private void GetFiles(DirectoryInfo di, string searchPattern, ref
ArrayList MyFiles)
        {
            foreach (FileInfo fi in di.GetFiles(searchPattern))
            {
                //check if the last write time of the file was not 20
seconds ago or sooner.
                //this prevents a file currently being written to to be
added to the list of files.
                if (fi.LastWriteTime < DateTime.Now.AddSeconds(-20))
                {
                    MyFiles.Add(fi.FullName);
                }
            }
            // Search in subdirctories
            foreach (DirectoryInfo d in di.GetDirectories())
            {
                GetFiles(d, searchPattern, ref MyFiles);
            }

        }

If you really want to follow the route of keeping track of the time of
the last write, then why not use FileSystemWatcher class with
NotifyFilters.LastWrite? It will be much more efficient.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top