安全地复制文件 [英] safely copy files

查看:90
本文介绍了安全地复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,


我需要安全地将文件从一个目录复制到另一个目录。我需要

确保我没有锁定文件使其他人无法阅读。


我不确定这是否是默认行为如果我刚刚使用

File.Copy ...


文件相对较小。


例如,我可以在记事本中打开文件,其他人可以编辑

给它,重命名,删除它。这些操作不受我的影响

在记事本中打开它。


感谢您的任何建议。


-Markus_R

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 behavior if I just used
File.Copy...

The files are relatively small.

For example, i can open up a file in notepad and someone else can edit
to it, rename it, delete it. Those operations are not affected by me
having it open in notepad.

Thanks for any advice.

-Markus_R

推荐答案

Markus,


如果你要复制文件,那么你绝对需要锁定

文件。如果有人在您复制它的过程中删除了该文件,那么当它被移动时你就会破坏该文件(因为你好了b / b
)能够在复制时阅读它。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" MarkusR" < ma ******* @ gmail.comwrote in message

news:11 ********************** @ i3g2000cwc.googlegro ups.com ...
Markus,

If you are copying the file over, then you absolutely need to have the
file locked. If someone deletes the file in the middle of you copying it,
then you are going to corrupt the file when it is moved over (since you
won''t be able to read it while copying).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MarkusR" <ma*******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

美好的一天,


我需要安全地将文件从一个目录复制到另一个。我需要

确保我没有锁定文件使其他人无法阅读。


我不确定这是否是默认行为如果我刚刚使用

File.Copy ...


文件相对较小。


例如,我可以在记事本中打开文件,其他人可以编辑

给它,重命名,删除它。这些操作不受我的影响

在记事本中打开它。


感谢您的任何建议。


-Markus_R
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 behavior if I just used
File.Copy...

The files are relatively small.

For example, i can open up a file in notepad and someone else can edit
to it, rename it, delete it. Those operations are not affected by me
having it open in notepad.

Thanks for any advice.

-Markus_R



我不同意,也不会说绝对。我可以看到如果文件更改或被删除,复制

应该回退的情况,这样复制是取消b
如果文件更改则删除半复制文件(

已被锁定以进行修改)或者如果文件被删除,我可能会误会

但我相信Windows用户shell就是这样的场景,两者都是文件

资源管理器和XCopy。


Jon


" Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com写在

消息新闻:uX ************** @ TK2MSFTNGP04.phx.gbl ...
I disagree and wouldn''t say "absolutely". I can see scenarios where copying
should fall back if the file changes or is removed, such that copying is
cancelled and the half-copied file is removed if the file changed (was
locked for modification) or if the file was removed, and I could be mistaken
but I believe the Windows user shell is among such scenarios, both File
Explorer and XCopy.

Jon

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uX**************@TK2MSFTNGP04.phx.gbl...

Markus,


如果你复制文件,那么你绝对需要

文件已锁定。如果有人在您复制它的过程中删除了该文件,那么当它被移动时你就会破坏该文件(因为你好了b / b
)能够在复制时阅读它。


-

- Nicholas Paldino [.NET / C#MVP]

- < a href =mailto:mv*@spam.guard.caspershouse.com> mv*@spam.guard.caspershouse.com

" MarkusR" < ma ******* @ gmail.comwrote in message

news:11 ********************** @ i3g2000cwc.googlegro ups.com ...
Markus,

If you are copying the file over, then you absolutely need to have the
file locked. If someone deletes the file in the middle of you copying it,
then you are going to corrupt the file when it is moved over (since you
won''t be able to read it while copying).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MarkusR" <ma*******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

>美好的一天,

我需要安全地将文件从一个目录复制到另一个目录。我需要确保我没有锁定文件使其他人无法阅读。

如果我刚使用
文件,我不确定这是否是默认行为。复制......

文件相对较小。

例如,我可以在记事本中打开文件,其他人可以编辑它,重命名它,删除它。这些操作不受我的影响
在记事本中打开它。

感谢您的任何建议。

-Markus_R
>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 behavior if I just used
File.Copy...

The files are relatively small.

For example, i can open up a file in notepad and someone else can edit
to it, rename it, delete it. Those operations are not affected by me
having it open in notepad.

Thanks for any advice.

-Markus_R






" MarkusR" < ma ******* @ gmail.comwrote in message

news:11 ********************** @ i3g2000cwc.googlegro ups.com ...

|美好的一天,

|

|我需要安全地将文件从一个目录复制到另一个目录。我需要

|确保我没有锁定文件使其他人无法阅读。

|

|如果我刚刚使用

|,我不确定这是否是默认行为File.Copy ...

|


File.Copy以ReadOnly模式打开文件,这意味着另一个

应用程序打开文件,必须为共享读取

访问权限。如果另一个应用程序以独占模式打开文件(没有

共享读取),File.Copy将失败。


|文件相对较小。

|

|例如,我可以在记事本中打开文件,其他人可以编辑

|它,重命名,删除它。那些操作不受我的影响

|在记事本中打开它。

|


原因是记事本不保持文件打开,它打开

文件,将其内容读入缓冲区并关闭文件。当你使用记事本保存文件时,你所做的只是将修改后的缓冲区保存回文件,如果缓冲区没有改变,文件就不会甚至写到。

这意味着记事本甚至不关心共享,你可以让多个用户在记事本中编辑文件 - 最后一个作者赢得一切。


威利。


"MarkusR" <ma*******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
| 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 behavior if I just used
| File.Copy...
|

File.Copy opens the file in ReadOnly mode, that means that another
application that has the file open, must have done this for shared read
access. If another application has opened the file in exclusive mode (no
shared read), File.Copy will fail.

| The files are relatively small.
|
| For example, i can open up a file in notepad and someone else can edit
| to it, rename it, delete it. Those operations are not affected by me
| having it open in notepad.
|

The reason for this is that notepad does NOT keep the file open, it opens
the file, reads it''s contents into a buffer and closes the file. When you
save a file using notepad, all you do is save the modified buffer back to
the file, if the buffer did not change, the file won''t even be written to.
That means that notepad doesn''t even care about sharing, you can have
multiple users editing a file in notepad - the last writer "wins it all".

Willy.


这篇关于安全地复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆