Checking if a file is read-only?

  • Thread starter Thread starter Jarod_24
  • Start date Start date
J

Jarod_24

What is wrong here. This thing allways returns true.
I want a simple way to see if a file is write protected.


'strFilename' holds the path for the file

Dim isWriteProtected As Boolean =
IO.File.GetAttributes(strFilename).IsDefined(FileAttribute.ReadOnly.GetType,
FileAttribute.ReadOnly)
If (isWriteProtected) Then Throw New Exception("File specified in 'Filename'
is write-protected")
 
What is wrong here. This thing allways returns true.
I want a simple way to see if a file is write protected.


'strFilename' holds the path for the file

Dim isWriteProtected As Boolean =
IO.File.GetAttributes(strFilename).IsDefined(FileAttribute.ReadOnly.GetType,
FileAttribute.ReadOnly)
If (isWriteProtected) Then Throw New Exception("File specified in 'Filename'
is write-protected")

Actually, it should always return true :) Your asking the the enum if
FileAttribute.ReadOnly is a member of the FileAttribute enum - which it
is...

What you want to do is more like:

If Not CBool(File.GetAttributes(strFilename) And FileAttribute.ReadOnly) Then
Throw New Exception(....)
End If
 
* "Jarod_24 said:
What is wrong here. This thing allways returns true.
I want a simple way to see if a file is write protected.


'strFilename' holds the path for the file

Dim isWriteProtected As Boolean =
IO.File.GetAttributes(strFilename).IsDefined(FileAttribute.ReadOnly.GetType,
FileAttribute.ReadOnly)
If (isWriteProtected) Then Throw New Exception("File specified in 'Filename'
is write-protected")

Your code doesn't make sense. It's only looking of the constant
'ReadOnly' is defined, which will be 'True'.

\\\
If (GetAttr("C:\foo.bat") And FileAttribute.ReadOnly) <> 0 Then
...
End If
///
 
Back
Top