Re: Checking if file being accessed

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Peter Duniho wrote:
On Tue, 10 Jun 2008 17:48:27 -0700, tshad <tshad@xxxxxxxxxxxxxx>
wrote:
I am trying to get access to a file that may still being written
because the
file is so large (7-10MB).


I get an error:

The process cannot access the file 'c:\TestDocs\XMLFiles\492172.XML'
because
it is being used by another process


This is when doing:

CheckFileBeingUsed(xmlFile); // below - should give me up to
15 seconds to finish writing the file

I don't understand this comment. The method you posted will not
return until it's given up or has opened the file itself. Either
way, how does that "give me up to 15 seconds to finish writing the
file"?

What is happening here is I am going to process this xml file that has lots
of images in it and is very large.

I am getting it too quick and I need to make the file has been completely
written and closed (so I don't get the file being accessed message) before I
start processing it.

The 15 seconds (probably not enough in some cases) is the 3 second delay for
5 loops. If I can access it right away, there would be no delay.

The problem is that the file is being written to and I am trying to open it
exclusively (FileShare.none). If someone already has it open (only when
being put in the folder), the this should give me an error and go to the
catch area. I assume that is how this is supposed to work (got the code
elsewhere).

But as you say later, I should just put the loop and try/catch around the
FileStream call, then when it becomes available I will already have it.


But, really...that doesn't matter. The important part is the rest of
the discussion:

fs = new FileStream(xmlFile, FileMode.Open,
System.IO.FileAccess.Read);

I tried the following routine which should get an error if still in
use. It
should run for 15 seconds (5 * 3), but even though the file is being
accessed (which I know because the FileStream error happens after the
call),
it jumps out after the first call and never does go to the catch.

The file is being accessed because you just opened it.

Hopefully, this helps illustrate the fallacy behind your approach. Don't
open a file just to check to see if you can open. Just try to
open it to _use_ it! In addition to the specific problem, the code
you posted also won't work because between the time you think you've
determined whether the file can be opened or not, the status of the
file could change. Most unhelpfully, you could find the file
available, but then it could become unavailable by the time you get
to try to open it.

In my case, this wouldn't happen. I am watching the folder with a
filewatcher Service and the only access would be the person dropping the
file in my folder and my accessing it.


If you want to include some sort of retry logic, so that you can
gracefully deal with situations where something else might also be
using the file, then do so. But once you've successfully opened the
file, just use the opened file. Anything less is just pointless
file-twiddling.

Probably right.

I am still curious as to why the FileShare.none wouldn't create an error, if
the file were still being written to.

Thanks,

Tom

Pete


.



Relevant Pages

  • Re: Remove GarageBand - save 2.5 GB of space?
    ... Can I safely uninstall GarageBand and delete its application support ... The Application Support folder can be ... > deleted but it WILL remove loops and software instruments that Logic ... I didn't think there was any duplication of resources, though shared, ...
    (comp.sys.mac.apps)
  • Re: How do you organize your writing?
    ... might be the best way to organize my writing. ... better to create a folder, and save each completed chapter as its own ... to email format. ... current projects by logging into my Gmail account. ...
    (misc.writing)
  • Re: Writers notebook, valuable tool or waste of time?
    ... You're supposed to spend some time each day just writing in your writer's notebook. ... snippets of overheard conversation, a memory, a description of how you're feeling at that moment, a 10 minute free-write -- whatever flits through your mind as you're sitting there with the notebook open. ... I used to use cardboard folder, but now I use see-through plastic ones. ... Invent a character who owns these things. ...
    (rec.arts.sf.composition)
  • Re: What is a writable CD folder?
    ... On the Windows XP Native CD Writing. ... George Hester ... >> Is this folder put on a CDR by Windows XP's native CD writing or is ...
    (microsoft.public.windowsxp.general)
  • Re: Deleting Mail Items - Code
    ... If Left= MailSubject Then ... Use For loops that count down or use Do loops that ... delete mail items from a selected mail folder. ... If there is only 1 it fails to delete it. ...
    (microsoft.public.outlook.program_vba)