.net - FileInfo.IsReadOnly 与 FileAttributes.ReadOnly

标签 .net vb.net file-attributes system.io.fileinfo

这两种检查文件是否只读的方式有区别吗?

Dim fi As New FileInfo("myfile.txt")

' getting it from FileInfo
Dim ro As Boolean = fi.IsReadOnly
' getting it from the attributes
Dim ro As Boolean = fi.Attributes.HasFlag(FileAttributes.ReadOnly)

如果不是,为什么会有两种不同的可能性?

最佳答案

嗯,根据.NET source code IsReadOnly 属性只检查文件的属性。

这是特定的属性:

public bool IsReadOnly {
  get {
     return (Attributes & FileAttributes.ReadOnly) != 0;
  }
  set {
     if (value)
       Attributes |= FileAttributes.ReadOnly;
     else
       Attributes &= ~FileAttributes.ReadOnly;
     }
}

这转换为以下 VB.Net 代码

Public Property IsReadOnly() As Boolean
    Get
        Return (Attributes And FileAttributes.[ReadOnly]) <> 0
    End Get
    Set
        If value Then
            Attributes = Attributes Or FileAttributes.[ReadOnly]
        Else
            Attributes = Attributes And Not FileAttributes.[ReadOnly]
        End If
    End Set
End Property

至于为什么有多种方法,这个随处可见。例如,您可以使用 StringBuilder.append("abc"& VbCrLf)StringBuilder.appendLine("abc")

关于.net - FileInfo.IsReadOnly 与 FileAttributes.ReadOnly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793297/

相关文章:

.net - 生成 IEnumerable(Of T) 元素的所有唯一组合

linux - 如何将 "execute"属性设置为文件并在 Windows 的 SVN 中检查它?

.net - 使用 CLR 将数据从 Excel 导入到 SQL Server 时出错

c# - 来自 appsetting.json (.NET Core) 的 WCF 服务客户端配置

vb.net - If() 语句的奇怪行为

vb.net - 来自 UIElement 的 Cast(Of ?)

.net - .NET 应用程序的 native 访问冲突

c# - 需要了解简短的 ASP.NET 源代码

java - 在哪里可以找到属性名称列表?

.net - 如何查找 Windows 文件夹中的项目是否真的对用户隐藏?