简体   繁体   English

使用C#锁定文件

[英]Lock file using C#

I saw this question: Prevent a user from deleting, moving or renaming a file , but it doesn't meet my needs. 我看到了这个问题: 防止用户删除,移动或重命名文件 ,但这不符合我的需求。
If I understood correctly, as long as that stream is opened, the file is locked. 如果我理解正确,只要打开该流,该文件就会被锁定。

I want to have a list of files in my computer, inside some folder, that cannot be edited, or renamed . 我想在计算机上的某个文件夹中有一个文件列表,这些文件无法编辑或重命名
The edit prevention is easy - I just set System.IO.FileAttributes.ReadOnly (Works well) 防止编辑很容易-我只需设置System.IO.FileAttributes.ReadOnly (效果很好)

Now I need to prevent a rename of a files and directories 现在,我需要防止文件和目录的重命名

My understanding is rename or move are both delete operations at the core. 我的理解是重命名或移动都是删除操作的核心。 You could try to catch and prevent instead. 您可以尝试捕捉并预防。 I'm not sure you can really do what you want to do there doesn't seem to be a flag for it. 我不确定您是否真的可以做您想做的事情,但似乎并没有为此做准备。

http://msdn.microsoft.com/en-us/library/system.io.fileattributes(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.io.fileattributes(v=vs.110).aspx

This might help. 这可能会有所帮助。 How to prevent or intercept a call to Directory.Delete(path, true) 如何阻止或拦截对Directory.Delete(path,true)的调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM