By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

Copy open files?

P: n/a
jim
Is there a way (using VB.Net or C#) to copy open or locked files?

Thanks!

jim
Apr 8 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Apr 8, 6:20 pm, "jim" <j...@home.netwrote:
Is there a way (using VB.Net or C#) to copy open or locked files?

Thanks!

jim
Look at this:
http://www.java2s.com/Code/VB/File-D...nlockafile.htm
Apr 8 '08 #2

P: n/a
On Apr 8, 11:20*am, "jim" <j...@home.netwrote:
Is there a way (using VB.Net or C#) to copy open or locked files?

Thanks!

jim
Hi,

No, if the file is locked by another process you cannot access it
Apr 8 '08 #3

P: n/a
On Apr 8, 12:45*pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
On Apr 8, 6:20 pm, "jim" <j...@home.netwrote:
Is there a way (using VB.Net or C#) to copy open or locked files?
Thanks!
jim

Look at this:http://www.java2s.com/Code/VB/File-D...nlockafile.htm
That shows how to lock/unlock a file, not how to read an already
locked file (most probably locked in another process)
Apr 8 '08 #4

P: n/a
AFAIK no (except perhaps using a very low level API (I'm thinking about
backup specific APIs)).

You may want to elaborate on what you are trying to do but it looks like
something unusual...
"jim" <ji*@home.neta écrit dans le message de news:
J2*****************@bignews5.bellsouth.net...
Is there a way (using VB.Net or C#) to copy open or locked files?

Thanks!

jim

Apr 8 '08 #5

P: n/a
On Apr 8, 8:43 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Apr 8, 12:45 pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
On Apr 8, 6:20 pm, "jim" <j...@home.netwrote:
Is there a way (using VB.Net or C#) to copy open or locked files?
Thanks!
jim
Look at this:http://www.java2s.com/Code/VB/File-D...nlockafile.htm

That shows how to lock/unlock a file, not how to read an already
locked file (most probably locked in another process)
Yes, i just wanted to point out that he could unlock the file using
the code then he could copy it.

(PS: The thread is a cross-post)
Apr 8 '08 #6

P: n/a
"kimiraikkonen" <ki*************@gmail.comwrote in message
news:a6**********************************@z24g2000 prf.googlegroups.com...
On Apr 8, 8:43 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
>On Apr 8, 12:45 pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
On Apr 8, 6:20 pm, "jim" <j...@home.netwrote:
Is there a way (using VB.Net or C#) to copy open or locked files?
Thanks!
jim
Look at
this:http://www.java2s.com/Code/VB/File-D...nlockafile.htm

That shows how to lock/unlock a file, not how to read an already
locked file (most probably locked in another process)

Yes, i just wanted to point out that he could unlock the file using
the code then he could copy it.
No you can't "unlock" a file "locked" by another process. Where "locked"
means opened exclusively, that is, non shared.
You can only use the "shadow copy services " to copy such file(s). The
shadow copy services API's aren't covered by the framework however.
Willy.

Apr 8 '08 #7

P: n/a
The Volume Shadow Copy service supports backing up locked files. There is an
API for this, which I have used via a batch file with script to backup the
Registry, but that's about as much as I can tell you.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
"jim" wrote:
Is there a way (using VB.Net or C#) to copy open or locked files?

Thanks!

jim
Apr 8 '08 #8

P: n/a
Jim,

You can use the CopyFileA Win32 API function to copy open files:

Private Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal bFailIfExists As Integer) As Integer

Here is an example call:
intResult = apiCopyFile(strSource, strDest, 0)

I don't know of a way to do this with managed code.

Best Regards,
Wayne
www.plotstream.com
"jim" <ji*@home.netwrote in message
news:J2*****************@bignews5.bellsouth.net...
Is there a way (using VB.Net or C#) to copy open or locked files?

Thanks!

jim
Apr 8 '08 #9

P: n/a
"Wayne" <me@nospam.comwrote in message
news:OE**************@TK2MSFTNGP02.phx.gbl...
Jim,

You can use the CopyFileA Win32 API function to copy open files:

Private Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal bFailIfExists As Integer) As Integer

Here is an example call:
intResult = apiCopyFile(strSource, strDest, 0)

I don't know of a way to do this with managed code.
What makes you think you can copy "locked" files by calling this API, which
is exactly the API wrapped by System.Io.File.Copy.

Willy.
Apr 8 '08 #10

P: n/a
On Apr 8, 2:30*pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
On Apr 8, 8:43 pm, "Ignacio Machin ( .NET/ C# MVP )"

<ignacio.mac...@gmail.comwrote:
On Apr 8, 12:45 pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
On Apr 8, 6:20 pm, "jim" <j...@home.netwrote:
Is there a way (using VB.Net or C#) to copy open or locked files?
Thanks!
jim
Look at this:http://www.java2s.com/Code/VB/File-D...nlockafile.htm
That shows how to lock/unlock a file, not how to read an already
locked file (most probably locked in another process)

Yes, i just wanted to point out that he could unlock the file using
the code then he could copy it.

(PS: The thread is a *cross-post)
Hi,

You can only unlock a file that you previously locked . Not only that,
but you must also use the same reference.
Apr 8 '08 #11

P: n/a
Willy,

This seems to work fine for me using Vista Pro. I have tried it on a couple
of different file types/applications (MS Word, AutoCAD, etc.) with open
files and this seemed to copy them anyway. The function returns a 1 and the
file copy is created. I tried deleting the files and they are definitely
locked. No dice there. Perhaps this is a Vista phenomenon?

Best Regards,
Wayne
www.plotstream.com
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Wayne" <me@nospam.comwrote in message
news:OE**************@TK2MSFTNGP02.phx.gbl...
>Jim,

You can use the CopyFileA Win32 API function to copy open files:

Private Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA"
_
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal bFailIfExists As Integer) As Integer

Here is an example call:
intResult = apiCopyFile(strSource, strDest, 0)

I don't know of a way to do this with managed code.

What makes you think you can copy "locked" files by calling this API,
which is exactly the API wrapped by System.Io.File.Copy.

Willy.

Apr 8 '08 #12

P: n/a
"Wayne" <me@nospam.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Willy,

This seems to work fine for me using Vista Pro. I have tried it on a
couple of different file types/applications (MS Word, AutoCAD, etc.) with
open files and this seemed to copy them anyway. The function returns a 1
and the file copy is created. I tried deleting the files and they are
definitely locked. No dice there. Perhaps this is a Vista phenomenon?
No, Vista behaves just like all other NT based OS version as far as File IO
goes..
None of the applications you mention do "lock" the files, nor do they keep
the files open when editing, with "locking" I mean open the file in
exclusive mode (not-shared), it's obvious that when you open a file for
shared read access that an other process can copy the file. Deleting a file
is a different matter, failure to delete is not due to the fact that the
file locked!

Try with a simple C# to open a file for read/write access with sharability
set to none and keep it open while you try to copy the file, you won't be
able to copy the file. Note that makes little sense to copy a file that is
open in another process, you are never guaranteed that the copy is logically
consistent. That's exactly why the shadow copy API's were invented.

Willy.


Apr 8 '08 #13

P: n/a
Willy Denoyette [MVP] wrote:
"Wayne" <me@nospam.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Willy,

This seems to work fine for me using Vista Pro. I have tried it on a
couple of different file types/applications (MS Word, AutoCAD, etc.)
with open files and this seemed to copy them anyway. The function
returns a 1 and the file copy is created. I tried deleting the files
and they are definitely locked. No dice there. Perhaps this is a
Vista phenomenon?

No, Vista behaves just like all other NT based OS version as far as
File IO goes..
None of the applications you mention do "lock" the files, nor do they
keep the files open when editing, with "locking" I mean open the file
in exclusive mode (not-shared), it's obvious that when you open a
file for shared read access that an other process can copy the file.
Deleting a file is a different matter, failure to delete is not due
to the fact that the file locked!
But "locked" as you define it *is* the same mechanism that makes the files
undeletable:

CreateFile with SHARE_READ and without SHARE_DELETE -- The behavior
described by Wayne
CreateFile without SHARE_READ -- The behavior described by Willy, CopyFileEx
API won't work

What other explanation do you have for failure to delete, except that the
file is open without SHARE_DELETE?
Apr 8 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.