Home All Groups Group Topic Archive Search About

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

Author
2 Feb 2006 9:54 PM
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

Author
3 Feb 2006 3:45 AM
tomb
Why are you opening the file?  Why not just copy it to the new location?

Tom

Eric Robinson wrote:

Show quoteHide quote
>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
>
>

>
Author
3 Feb 2006 4:05 AM
David Browne
"Eric Robinson" <eric @ pmcipa..{com}> wrote in message
news:%bvEf.2300$gl3.1821@fe06.lga...
> 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclassctortopic6.asp

David
Author
3 Feb 2006 10:31 AM
Armin Zingler
"David Browne" <davidbaxterbrowne no potted m***@hotmail.com> schrieb
>
> 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
Author
3 Feb 2006 11:45 AM
Dennis
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.
--
Dennis in Houston


Show quoteHide quote
"Eric Robinson" wrote:

> 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
>
>
>
Author
3 Feb 2006 6:13 PM
Eric Robinson
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?


Show quoteHide quote
"Eric Robinson" <eric @ pmcipa..{com}> wrote in message
news:%bvEf.2300$gl3.1821@fe06.lga...
> 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
>
Author
6 Feb 2006 8:07 PM
tomb
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

Eric Robinson wrote:

Show quoteHide quote
>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?
>
>
>"Eric Robinson" <eric @ pmcipa..{com}> wrote in message
>news:%bvEf.2300$gl3.1821@fe06.lga...

>
>>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
>>
>>   
>>
>
>

>
Author
6 Feb 2006 10:09 PM
CMM
"tomb" <t***@technetcenter.com> wrote in message
news:i%NFf.8$S03.7@bignews1.bellsouth.net...
<snip>
> 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.
Author
7 Feb 2006 6:20 PM
tomb
Thanks, didn't realize all that.

Tom


CMM wrote:

Show quoteHide quote
>"tomb" <t***@technetcenter.com> wrote in message
>news:i%NFf.8$S03.7@bignews1.bellsouth.net...
><snip>

>
>>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.
>
>

>
Author
3 Feb 2006 10:51 PM
CMM
Try researching into WinXP's ShadowCopy API... it's what other programs
(like backups) use to copy files that may be in use and prevent programs
from blowing if they try to access the file while you're copying it.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_shadowcopy.asp?frame=true
Maybe there's a .NET friendly wrapper for it somewhere? That'd be nice.