Newbie Question: How To Open A File Non-Exclusively?

  • Thread starter Thread starter Eric Robinson
  • Start date Start date
E

Eric Robinson

Hello, I want to copy files from one directory to another, but the files in
the first directory are occasionally accessed by a process outside my
control. Can I can open files for reading in a way that will not interfere
with the other process? My process will never change files. I don't care if
the other process changes the file while I have it open. If a file changes
while it is being copied, I can just copy it again. Is this possible?

--Eric
 
Eric Robinson said:
Hello, I want to copy files from one directory to another, but the files
in the first directory are occasionally accessed by a process outside my
control. Can I can open files for reading in a way that will not interfere
with the other process? My process will never change files. I don't care
if the other process changes the file while I have it open. If a file
changes while it is being copied, I can just copy it again. Is this
possible?

Perhaps. When you open a file you declare how you are willing to share the
file. So does the other process. If the you both open the file with
compatible flags, then yes. But if the other processes aren't willing to
share then no.

Anyway you woud use this overload of System.IO.FileStream constructor to
specify the file access and sharing mode:


Public Sub New( _
ByVal path As String, _
ByVal mode As FileMode, _
ByVal access As FileAccess, _
ByVal share As FileShare _
)

http://msdn.microsoft.com/library/d...ml/frlrfsystemiofilestreamclassctortopic6.asp

David
 
David Browne said:
Perhaps. When you open a file you declare how you are willing to
share the file. So does the other process. If the you both open
the file with compatible flags, then yes. But if the other
processes aren't willing to share then no.

Very good and short explanation! :-)


Armin
 
It depends on how the other processes open the file. If they open it
exclusvely then you can't access it unil the process that opened it releases
it. You can however, trap the access error and skip the file. Keep the
skipped files in an arraylist then come back to them when all the other file
are copied.
 
tomb, you have to open a file to copy it, right? hence my question. Copying
is opening.

Dennis, your "trap and put in array" aproach is exactly what we are already
doing. It allows me to gracefully handle conflicts when I try to open a file
but the other process already has it. The problem I'm trying to avoid is
having the other process throw an error when it tries to open a file while I
am copying it.

David, thanks for the code. If I understand you correctly, I should open the
file in shared mode and hope the other application does the same. If it
doesn't, and I cannot get the developers of the other application to change
their ways, I'm stuck. Correct?

Here's a related question: how to open file agents work? Presumably the
makers of backup software have found a way around this problem?
 
No, you don't have to open the file to copy it. You just copy it.
Windows will allow you to copy a file that is being used, but you may
have to use a system dll to do it. I never tried it with .Net or any
other flavor of VB.

Tom
 
Windows will allow you to copy a file that is being used, but you may have
to use a system dll to do it. <snip>

Not true. It depends on how the file is locked. If a program has a file open
and it locks it locked Deny-Read it cannot be copied. No API will allow yout
to copy it AFAIK... except the ShadowCopy API's on WinXP and WinServer 2k3.

For instance, Outlook's PST data files cannot be copied in any manner while
Outlook is open. On the other hand, loaded dll's and programs can be copied
because they're only locked as Deny-Write. Microsoft Office documents that
are opened can also be copied by because Word and Excel etc create a hidden
copy that they use and the original is kept Allow-Read.
 
Thanks, didn't realize all that.

Tom

Not true. It depends on how the file is locked. If a program has a file open
and it locks it locked Deny-Read it cannot be copied. No API will allow yout
to copy it AFAIK... except the ShadowCopy API's on WinXP and WinServer 2k3.

For instance, Outlook's PST data files cannot be copied in any manner while
Outlook is open. On the other hand, loaded dll's and programs can be copied
because they're only locked as Deny-Write. Microsoft Office documents that
are opened can also be copied by because Word and Excel etc create a hidden
copy that they use and the original is kept Allow-Read.
 
Back
Top