在 C# 中使文件可写的最佳方法
我正在尝试设置标志,当您在文件上右键单击 \ Properties
时,会出现Read Only
复选框。
谢谢!
I'm trying to set flag that causes the Read Only
check box to appear when you right click \ Properties
on a file.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
两种方法:
或
FileInfo
上的IsReadOnly
属性本质上执行在第二种方法中必须手动执行的位翻转。Two ways:
or
The
IsReadOnly
property onFileInfo
essentially does the bit-flipping you would have to do manually in the second method.要设置只读标志,实际上使文件不可写:
删除只读标志,实际上使文件可写:
切换只读标志,使其与现在的情况相反:
这基本上是有效的位掩码。 您设置一个特定位来设置只读标志,清除它以删除该标志。
请注意,上述代码不会更改文件的任何其他属性。 换句话说,如果文件在执行上述代码之前被隐藏,那么它之后也将保持隐藏状态。 如果您只是将文件属性设置为
.Normal
或.ReadOnly
您最终可能会在此过程中丢失其他标志。To set the read-only flag, in effect making the file non-writeable:
To remove the read-only flag, in effect making the file writeable:
To toggle the read-only flag, making it the opposite of whatever it is right now:
This is basically bitmasks in effect. You set a specific bit to set the read-only flag, you clear it to remove the flag.
Note that the above code will not change any other properties of the file. In other words, if the file was hidden before you executed the above code, it will stay hidden afterwards as well. If you simply set the file attributes to
.Normal
or.ReadOnly
you might end up losing other flags in the process.C# :
C# :
一行答案(不更改其他文件属性):
One-line answer (without changing other file attributes):