windows锁定文件夹_如何在Windows中锁定文件以阻止删除或覆盖?

时间:2024-03-27 16:37:21
windows锁定文件夹_如何在Windows中锁定文件以阻止删除或覆盖?

windows锁定文件夹

windows锁定文件夹_如何在Windows中锁定文件以阻止删除或覆盖?

While there are plenty of third-party programs to accomplish a task, sometimes you just want to use a built-in method to do the same thing. Today’s SuperUser Q&A post helps a reader find an awesome built-in solution to keep his files from being deleted or overwritten.

尽管有很多第三方程序可以完成任务,但有时您只想使用内置方法来完成相同的任务。 今天的“超级用户”问答文章可帮助读者找到一个很棒的内置解决方案,以防止其文件被删除或覆盖。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

Notepad screenshot courtesy of Dan (SuperUser).

记事本屏幕截图由Dan(SuperUser)提供

问题 (The Question)

SuperUser reader boboes wants to know if there is a built-in method in Windows 7 and newer editions to lock a file and keep it from being overwritten or deleted:

SuperUser阅读器boboes想知道Windows 7和更高版本中是否有内置方法来锁定文件并防止其被覆盖或删除:

Is there a way to lock a file with in-house means so that it cannot be deleted or overwritten? I need to temporarily lock files while testing copy scripts in order to check the error handling in those scripts. Until Windows XP, I would load a file in debug.exe to lock it. Is there a way to do this in Windows 7 and newer editions?

有没有一种使用内部手段锁定文件的方法,以使它不能被删除或覆盖? 我需要在测试复制脚本时临时锁定文件,以检查这些脚本中的错误处理。 在Windows XP之前,我将在debug.exe中加载文件以将其锁定。 在Windows 7和更高版本中,有没有办法做到这一点?

I know that there are programs for doing this. My question is if there is a built in mechanism in Windows 7 and newer editions. Sometimes I have to check a script on a PC and do not want to install new programs for that.

我知道有执行此操作的程序。 我的问题是Windows 7和更高版本中是否存在内置机制。 有时我必须在PC上检查脚本,并且不想为此安装新程序。

There are also some good suggestions here: How to purposefully exclusively lock a file? Unfortunately, they require 3rd party tools or changing the file to a locked state.

这里也有一些好的建议: 如何有目的地独占锁定文件? 不幸的是,他们需要第三方工具或将文件更改为锁定状态。

Is there a built-in method for doing this in Windows 7 and newer editions?

在Windows 7和更高版本中是否有内置方法可以执行此操作?

答案 (The Answer)

SuperUser contributors Dan and Breakthrough have the answer for us. First up, Dan:

超级用户贡献者Dan和Breakthrough为我们找到了答案。 首先,丹:

I think PowerShell is probably the neatest way to accomplish this. Something like the following:

我认为PowerShell可能是完成此任务的最简洁的方法。 类似于以下内容:

windows锁定文件夹_如何在Windows中锁定文件以阻止删除或覆盖?

While paused, the above script causes the following prompt when attempting to open up “myfile.txt”:

暂停时,以上脚本在尝试打开“ myfile.txt”时引起以下提示:

windows锁定文件夹_如何在Windows中锁定文件以阻止删除或覆盖?

Followed by the answer from Breakthrough:

接下来是突破的答案:

For the curious, here is the MSDN link to the File::Open method detailing the parameters of the call (specifically, the last argument ‘None’, declines sharing the file with any other threads/processes). Likewise, provisions exist to lock the file for read/write only, or a combination thereof.

出于好奇,这里是MSDN到File :: Open方法的链接,详细描述了调用的参数(特别是最后一个参数“ None” 拒绝与任何其他线程/进程共享文件 )。 同样,存在将文件锁定为仅读/写或其组合的措施。



Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

翻译自: https://www.howtogeek.com/206875/how-do-you-lock-a-file-in-windows-to-block-deletion-or-overwriting/

windows锁定文件夹