Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileInfo.IsReadOnly versus FileAttributes.ReadOnly

Is there any difference between these two ways of checking whether a file is read-only?

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)

If not why are there two different possibilities?

like image 323
jor Avatar asked Mar 10 '26 00:03

jor


1 Answers

Well, according to the .NET source code the IsReadOnly property just checks the attributes of the file.

Here is the particular property:

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

This translates to the following VB.Net Code

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

As to why there are multiple methods, this can be seen everywhere. For example, you can use StringBuilder.append("abc" & VbCrLf) or StringBuilder.appendLine("abc")

like image 153
Dan Drews Avatar answered Mar 12 '26 14:03

Dan Drews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!