473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Copy open files?

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

Thanks!

jim
Apr 8 '08 #1
13 7892
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
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
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
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
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
"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
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
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
"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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kay Lee | last post by:
Hi, I looked up os module to find out some method to move and copy files in python, but os doesn't support such methods. Is there any way to move & copy files in python? Thanks in adv.
0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
5
by: Verane | last post by:
Hi, I have read the thread named "Could not copy temporary files to the output directory" on this newsgroup. And I have the same symptoms on my machine. But I didn't find any solution suitable for...
8
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
3
by: Jim McGivney | last post by:
I am a ASP.net 2.0 NewBee I have a simple website I developed in Web Developer Express. I test it with CTRL-F5 and it works. I have found out that I can not Web Copy it to my hosted web site. I...
9
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that...
11
by: MarkusR | last post by:
Good day, I need to safely copy files from one directory to another. I need to make sure that I do not lock the file making it unreadable by others. I am not sure if this is the default...
8
by: jonathan184 | last post by:
Hi I am trying to copyfiles and dirs keeping there permissions intact and copy to multiple destinations which are on a data.txt file on the txt file i put test1 test2 So where ami going wrong?...
13
by: jim | last post by:
Is there a way (using VB.Net or C#) to copy open or locked files? Thanks! jim
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.