473,663 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I check for complete file

Hello!

I'm trying to create a program that will watch a directory and when a file
is created print that file. I have used FileSystemWatch er for watching the
directory and I get an created event.

The problem is that I don't know when the file is complete when using
Explorer copy or move.

I have tried to use FileIOPermissio n but I always get that the file is
unlocked.

How do I check for file complete?

// Anders
--
English isn't my first, or second, language.
So anything rude or strange are due to the translation

private static bool isFileLocked(st ring path)
{
FileIOPermissio n _available = new FileIOPermissio n( PermissionState .None);
// blank slate
try
{
_available.SetP athList( FileIOPermissio nAccess.Write, path);
_available.Dema nd( ); // does file permit shared writing?
Console.WriteLi ne("isFileLocke d: false ");
return false;
}
catch ( SecurityExcepti on _se)
{
Console.WriteLi ne("isFileLocke d: true ");
return true;
}
}
Nov 17 '05 #1
12 15894
I have never, personally needed to do this before, though I remember seeing
other messages about this in one of these newsgroups a while back.

As I remember it, one of two things can be done:
1- actually try to open the file for writing (though i don't know how this
is different than what you did)
2- periodically check the file size to see if it is still increasing (ie. in
the process of being written)

sorry i can't be more help than that...

scott
"Anders Eriksson" <an*****@gmail. com> wrote in message
news:1k******** *******@moratek nikutveckling.s e...
Hello!

I'm trying to create a program that will watch a directory and when a file
is created print that file. I have used FileSystemWatch er for watching the
directory and I get an created event.

The problem is that I don't know when the file is complete when using
Explorer copy or move.

I have tried to use FileIOPermissio n but I always get that the file is
unlocked.

How do I check for file complete?

// Anders
--
English isn't my first, or second, language.
So anything rude or strange are due to the translation

private static bool isFileLocked(st ring path)
{
FileIOPermissio n _available = new FileIOPermissio n( PermissionState .None);
// blank slate
try
{
_available.SetP athList( FileIOPermissio nAccess.Write, path);
_available.Dema nd( ); // does file permit shared writing?
Console.WriteLi ne("isFileLocke d: false ");
return false;
}
catch ( SecurityExcepti on _se)
{
Console.WriteLi ne("isFileLocke d: true ");
return true;
}
}

Nov 17 '05 #2
The cleanest way I have found to handle this (assuming that you are able to
do it in your situation) is to transmit a small control file to the folder
containing the name and or filetime of the real file to be transferred.
When the real file transfer is complete, you remove the small control file.
So all you would need to do is check for the presense of the control file.
Peter

"Anders Eriksson" <an*****@gmail. com> wrote in message
news:1k******** *******@moratek nikutveckling.s e...
Hello!

I'm trying to create a program that will watch a directory and when a file
is created print that file. I have used FileSystemWatch er for watching the
directory and I get an created event.

The problem is that I don't know when the file is complete when using
Explorer copy or move.

I have tried to use FileIOPermissio n but I always get that the file is
unlocked.

How do I check for file complete?

// Anders
--
English isn't my first, or second, language.
So anything rude or strange are due to the translation

private static bool isFileLocked(st ring path)
{
FileIOPermissio n _available = new FileIOPermissio n( PermissionState .None);
// blank slate
try
{
_available.SetP athList( FileIOPermissio nAccess.Write, path);
_available.Dema nd( ); // does file permit shared writing?
Console.WriteLi ne("isFileLocke d: false ");
return false;
}
catch ( SecurityExcepti on _se)
{
Console.WriteLi ne("isFileLocke d: true ");
return true;
}
}

Nov 17 '05 #3
I know that this doesn’t fix your problem however could you copy/move the
file to another folder on the same drive as the directory you are monitoring,
and once that operation is complete have the copier/mover make one final move
to your monitoring directory?

The nice thing about moving files on the same disk is that it is just a
pointer update as far as the system is concerned, the file does not change
locations physically so there is no delay between its first appearing in the
directory and it’s being fully accessible.

Brendan

"Anders Eriksson" wrote:
Hello!

I'm trying to create a program that will watch a directory and when a file
is created print that file. I have used FileSystemWatch er for watching the
directory and I get an created event.

The problem is that I don't know when the file is complete when using
Explorer copy or move.

I have tried to use FileIOPermissio n but I always get that the file is
unlocked.

How do I check for file complete?

// Anders
--
English isn't my first, or second, language.
So anything rude or strange are due to the translation

private static bool isFileLocked(st ring path)
{
FileIOPermissio n _available = new FileIOPermissio n( PermissionState .None);
// blank slate
try
{
_available.SetP athList( FileIOPermissio nAccess.Write, path);
_available.Dema nd( ); // does file permit shared writing?
Console.WriteLi ne("isFileLocke d: false ");
return false;
}
catch ( SecurityExcepti on _se)
{
Console.WriteLi ne("isFileLocke d: true ");
return true;
}
}

Nov 17 '05 #4
It might be very hairy, and there are certainly some drawbacks but...

Take a look at FSCTL_READ_USN_ JOURNAL and READ_USN_JOURNA L_DATA which can be
used with DeviceIoControl () to get NTFS change journal records. This
structure has a flag for specifying only returning with the change journal
records once the file is closed.

So in theory...

1. std notification from FileSystemWatch er.
2. acquire necessary info to build a READ_USN_JOURNA L_DATA structure
3. call into DeviceIoControl () with ReturnOnlyOnClo se set
4. discard results... at this point copy is done

This should work for copy, don't know about move.

Better yet, I think FileSystemWatch er is implemented using some of the NTFS
change journal magic... so look at extending it. Maybe by spelunking the IL
for FileSystemWatch er will help with understanding how MS did it.
"Anders Eriksson" wrote:
Hello!

I'm trying to create a program that will watch a directory and when a file
is created print that file. I have used FileSystemWatch er for watching the
directory and I get an created event.

The problem is that I don't know when the file is complete when using
Explorer copy or move.

I have tried to use FileIOPermissio n but I always get that the file is
unlocked.

How do I check for file complete?

// Anders
--
English isn't my first, or second, language.
So anything rude or strange are due to the translation

private static bool isFileLocked(st ring path)
{
FileIOPermissio n _available = new FileIOPermissio n( PermissionState .None);
// blank slate
try
{
_available.SetP athList( FileIOPermissio nAccess.Write, path);
_available.Dema nd( ); // does file permit shared writing?
Console.WriteLi ne("isFileLocke d: false ");
return false;
}
catch ( SecurityExcepti on _se)
{
Console.WriteLi ne("isFileLocke d: true ");
return true;
}
}

Nov 17 '05 #5

"Daymon" <Da****@discuss ions.microsoft. com> wrote in message
news:DB******** *************** ***********@mic rosoft.com...
It might be very hairy, and there are certainly some drawbacks but...

Take a look at FSCTL_READ_USN_ JOURNAL and READ_USN_JOURNA L_DATA which can
be
used with DeviceIoControl () to get NTFS change journal records. This
structure has a flag for specifying only returning with the change journal
records once the file is closed.

So in theory...

1. std notification from FileSystemWatch er.
2. acquire necessary info to build a READ_USN_JOURNA L_DATA structure
3. call into DeviceIoControl () with ReturnOnlyOnClo se set
4. discard results... at this point copy is done

This should work for copy, don't know about move.

Better yet, I think FileSystemWatch er is implemented using some of the
NTFS
change journal magic... so look at extending it. Maybe by spelunking the
IL
for FileSystemWatch er will help with understanding how MS did it.

No, the filesystem watcher does not rely on NTFS and NTFS Journaling. It
uses ReadDirectoryCh angesW,FindFirs tChangeNotifica tion and
FindNextChangeN otification as it must be usable on downlevel platforms too.

Willy.

Nov 17 '05 #6


"Willy Denoyette [MVP]" wrote:

"Daymon" <Da****@discuss ions.microsoft. com> wrote in message
news:DB******** *************** ***********@mic rosoft.com...
It might be very hairy, and there are certainly some drawbacks but...

Take a look at FSCTL_READ_USN_ JOURNAL and READ_USN_JOURNA L_DATA which can
be
used with DeviceIoControl () to get NTFS change journal records. This
structure has a flag for specifying only returning with the change journal
records once the file is closed.

So in theory...

1. std notification from FileSystemWatch er.
2. acquire necessary info to build a READ_USN_JOURNA L_DATA structure
3. call into DeviceIoControl () with ReturnOnlyOnClo se set
4. discard results... at this point copy is done

This should work for copy, don't know about move.

Better yet, I think FileSystemWatch er is implemented using some of the
NTFS
change journal magic... so look at extending it. Maybe by spelunking the
IL
for FileSystemWatch er will help with understanding how MS did it.

No, the filesystem watcher does not rely on NTFS and NTFS Journaling. It
uses ReadDirectoryCh angesW,FindFirs tChangeNotifica tion and
FindNextChangeN otification as it must be usable on downlevel platforms too.

Willy.


Good call.

Even so, would hooking the NTFS change journal be a good solution for this
type of problem. It seems as though a person is having to dig pretty deep
just to find out when a file got closed...

If it might be a reasonable idea, would it even work?

Nov 17 '05 #7
Yes... I was going to suggest a rename as the simplest solution, if you
have control over the file's producer.

My (traditional) solution has been this:

1. Producer writes file to target directory under a temporary name.
2. After flushing and closing the file, producer renames file to new
name, indicating that file is complete.
3. Consumer detects new file with desired name and immediately renames
file to a different temporary name (this is necessary only if you scale
up to multiple consumers). If rename is successful, consumer processes
file under temporary name.

Of course, this works only if you have control over the producer. If
you don't, then you have to resort to one of the fancier solutions
proposed here.

Nov 17 '05 #8

"Daymon" <Da****@discuss ions.microsoft. com> wrote in message
news:9B******** *************** ***********@mic rosoft.com...


"Willy Denoyette [MVP]" wrote:

"Daymon" <Da****@discuss ions.microsoft. com> wrote in message
news:DB******** *************** ***********@mic rosoft.com...
> It might be very hairy, and there are certainly some drawbacks but...
>
> Take a look at FSCTL_READ_USN_ JOURNAL and READ_USN_JOURNA L_DATA which
> can
> be
> used with DeviceIoControl () to get NTFS change journal records. This
> structure has a flag for specifying only returning with the change
> journal
> records once the file is closed.
>
> So in theory...
>
> 1. std notification from FileSystemWatch er.
> 2. acquire necessary info to build a READ_USN_JOURNA L_DATA structure
> 3. call into DeviceIoControl () with ReturnOnlyOnClo se set
> 4. discard results... at this point copy is done
>
> This should work for copy, don't know about move.
>
> Better yet, I think FileSystemWatch er is implemented using some of the
> NTFS
> change journal magic... so look at extending it. Maybe by spelunking
> the
> IL
> for FileSystemWatch er will help with understanding how MS did it.

No, the filesystem watcher does not rely on NTFS and NTFS Journaling. It
uses ReadDirectoryCh angesW,FindFirs tChangeNotifica tion and
FindNextChangeN otification as it must be usable on downlevel platforms
too.

Willy.


Good call.

Even so, would hooking the NTFS change journal be a good solution for this
type of problem. It seems as though a person is having to dig pretty deep
just to find out when a file got closed...

If it might be a reasonable idea, would it even work?


Well, I have done something lately with the NTFS change journal, not exactly
for the same purpose though, I had it working but I dropped it in favor of a
FS Filter Driver. The reason I didn't use change journal was for reasons of
maintenance issues with the journal streams. While it might be a workable
solution, there is an other options called ETW (Event Tracing for Windows)
which is event driven and provides very low overhead (by the way this is the
thing used by the VS2005 trace facility).

Willy.


Nov 17 '05 #9
On Tue, 4 Oct 2005 16:41:56 +0200, Scott wrote:

As I remember it, one of two things can be done:
1- actually try to open the file for writing (though i don't know how this
is different than what you did) I have now changed so I use the Win32 API CreateFile() and now got it to
work! To make this work I need to compile the program with /unsafe switch.

Is there a C# function that does the same as CreateFile?
2- periodically check the file size to see if it is still increasing (ie. in
the process of being written)

It seems like the size of the file is written when the file is created,
before the contents is copied.

// Anders
Nov 17 '05 #10

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

Similar topics

3
34741
by: newcomer | last post by:
Is there a way to check if a file exists in Javascript? This is what I'm trying to do: if(thisfile.htm exists) do this else do that
4
1975
by: Steph | last post by:
Hi, Users send files via FTP in a specific directory on our server. I need to create a program that checks out the arrival of a file (24/24, 7/7...) in the directory and then that performs some manipulations with this file. But, i need the file to be completely transfered before manipulating it. What is the best way to wait the transfer to be completed ? Thanks.
2
2107
by: Grey | last post by:
i have used asp.net to transfer a file from one server to another . i use the system.io.file.copy to copy the file. But i want to know that how can assure the file is transfer complete?? Do i need to check the file is exist in the target location??? any another smarter way to do that?? million thanks.
3
2570
by: HK guy | last post by:
How to write a program that can check the web page loading time? Before I have written a browser that use IE as the core, but it does not support frame. Since the documentcomplete event will trigger if the frame is completed. So i don't know how to get the time for the whole document complete. Or beside the browser, any other method to do this. Thank you.
26
2440
by: ValK | last post by:
Hi, Here is my problem. I need to check if scanner finished scanning the document. I'm using scanner interface that came with the scanner. I tried to open file and catch IO Exception for "The process cannot access the file ....., because it is being used by another process." It worked with .PDF but not with TIF files. It will allow me to open file while scanning.
5
1554
by: Paolo | last post by:
Hi all! I have to create an application that receive some packet from an interface and builds a file from those. In the header there are three fields: one is the total lenght of the file, one is the offset of the packet in the file and one is the lenght of the current packet. I made a function that gets all the packets and reconstruct the file. while(...) {
9
10261
by: Mark | last post by:
Hi all, This is something which has been bugging me for ages. How can I check if a file is already in use by a different program? It doesn't seem to matter which mode I pass to fopen, it will always allow me to open the file. I've tried r, r+, w, w+, a, and a+ but still the fopen call will not return null. Basically what I'm doing is using a kqueue to watch a particular
6
3675
by: test2000 | last post by:
Hello I'm trying to write a javascript that checks if a server/host is available. To be more precice. I would like to check if the browser can read an XML file from a server. If not the script should read the same XML file from a backup server. Does anyone have any idea how to achive that?? Any help is appreciated.
11
10336
by: Hendri Adriaens | last post by:
Hi, I use the following code to offer a download: header("Content-Type:$mimetype"); header("Content-Disposition: attachment; filename=$filename"); header("Cache-Control:must-revalidate,post-check=0,pre-check=0"); header("Content-Length:".filesize($temp_filename)); readfile_chunked($temp_filename);
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.