这两种检查文件是否为只读的方法有什么区别吗?
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)
如果不是,为什么会有两种不同的可能性?
这两种检查文件是否为只读的方法有什么区别吗?
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 源代码,该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")