3

ファイルが読み取り専用かどうかを確認するこれら 2 つの方法に違いはありますか?

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)

そうでない場合、なぜ2つの異なる可能性があるのでしょうか?

4

1 に答える 1

2

.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")

于 2015-04-22T12:24:23.340 に答える