473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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,
Aug 14 '08 #1
3 4116
Rogelio wrote:
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.

--
J.
Aug 14 '08 #2
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);
}

}

"Jeroen Mostert" wrote:
Rogelio wrote:
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.

--
J.
Aug 15 '08 #3
On Aug 15, 9:17*pm, Rogelio <Roge...@discussions.microsoft.comwrote:
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.
Aug 15 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan Kelley | last post by:
We have a multithreaded app that responds to events, and writes these events to a text file. This text file is used by an external system for further processing. We want to be able to write...
2
by: Suresh Gladstone | last post by:
Hi, This is a bit with versioning and installation of the .NET dlls. I want to perform the following, 1. A third party application will be invoking my .NET dll through COM interop . For this I...
2
by: faktujaa | last post by:
Hi All, I have to avoid cyclic reference in my DLLS. One way is to move the code to the common DLL. But now since i have written a lot of code, it would take a whole lot of time to do this. The...
8
by: Phoenix | last post by:
Here's a challenge that is killing me: I've got 2 web servers and a SQL Server and about 5,000 'users' who stay connected to the site all day. I have a page that is supposed to be 'real-time',...
10
by: Simon Elliott | last post by:
Is there a way at compile time to determine if a function has been declared? My specific reason for this is because of memicmp() and stricmp() which are declared in namespace std by some...
4
by: hype | last post by:
Hi, How can I find out how much space has been used in the log file and how much of it is free or yet to be used ? Thanks, Hype
9
by: pob | last post by:
I currently have a procedure that loops thru a recordset to determine what files need to be loaded to my database. The naming convention of the files has always been accounts.txt, namelist.txt,...
0
by: Gabriel Genellina | last post by:
En Mon, 26 May 2008 05:31:27 -0300, <Dominique.Holzwarth@ch.delarue.comescribió: Don't inherit from Exception - you should be able to log *any* exception, not only this specific one, I presume?...
8
johngault
by: johngault | last post by:
I've been working with this PHP page for several days now and I'm stumped. The page is supposed to allow the user to upload up to six images for their profile. When the user adds an image it (the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.