C# 使文件从只读读/写
如果 File.SetAttributes("C:\\myFile.txt", FileAttributes.ReadOnly);
将文件设置为只读,如果需要,如何将其设置回读/写?
我怀疑它会是 FileAttributes.Normal 但是这会改变文件的任何其他属性吗? MSDN 网站上没有非常详细的描述性注释...
文件正常,没有设置其他属性。这个属性是 仅在单独使用时有效。
谢谢
If File.SetAttributes("C:\\myFile.txt", FileAttributes.ReadOnly);
sets a file as read only, how do I set it back to read/write if I need to?
I suspect it would be FileAttributes.Normal
however will this change any other properties of the file? There isn't an awfully descriptive note on the MSDN site...
The file is normal and has no other attributes set. This attribute is
valid only if used alone.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要仅删除
ReadOnly
属性,您可以执行以下操作:这将删除
ReadOnly
属性,但保留文件中已存在的任何其他属性。To remove just the
ReadOnly
attribute, you'd do something like this:This will remove the
ReadOnly
attribute, but preserve any other attributes that already exist on the file.File.SetAttributes 替换文件上的所有属性。
设置和删除属性的正确方法是首先获取属性,应用更改,然后设置它们。
例如
File.SetAttributes replaces ALL attributes on the file.
The proper way to set and remove attributes is to first get the attributes, apply changes, and set them.
e.g.
我知道这已经很晚了,但我想分享我的解决方案希望它能帮助其他人。我需要类似的东西,我完成的方法是在
FileInfo
上设置IsReadOnly
属性。I understand this is very late, but I wanted to share my solution hoping it helps others. I needed something similar and the way I accomplished was by setting the
IsReadOnly
property onFileInfo
.