m_ArchiveBoolean, m_HiddenBoolean, m_ReadOnlyBoolean, m_IndexedBoolean, m_CompressedBoolean are all form level booleans that's set from their appropriate Checkboxes on the form before this code runs in a BackgroundWorker.
Why is it that this code works:
Where as this code doesn't:
In this 2nd one attributes are being changed, but they're the wrong ones and I can't pinpoint it right off hand.
Edit: What the overall goal of this is that if the user has the attribute checkbox checked then the file needs to have it set, other wise the attribute (if it's present) needs to be unset.
Why is it that this code works:
VB.NET:
Try
Dim fi As New FileInfo(element)
With fi
If m_ArchiveBoolean = False Then
If (.Attributes And IO.FileAttributes.Archive) = IO.FileAttributes.Archive Then .Attributes = .Attributes Xor IO.FileAttributes.Archive 'Turn off
Else
.Attributes = .Attributes Or IO.FileAttributes.Archive
End If
If m_HiddenBoolean = False Then
If (.Attributes And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then .Attributes = .Attributes Xor IO.FileAttributes.Hidden 'Turn off
Else
.Attributes = .Attributes Or System.IO.FileAttributes.Hidden
End If
If m_ReadOnlyBoolean = False Then
If (.Attributes And IO.FileAttributes.ReadOnly) = IO.FileAttributes.ReadOnly Then .Attributes = .Attributes Xor IO.FileAttributes.ReadOnly 'Turn off
Else
.Attributes = .Attributes Or System.IO.FileAttributes.ReadOnly
End If
If m_IndexedBoolean = True Then
If (.Attributes And IO.FileAttributes.NotContentIndexed) = IO.FileAttributes.NotContentIndexed Then .Attributes = .Attributes Xor IO.FileAttributes.NotContentIndexed 'Turn off
Else
.Attributes = .Attributes Or System.IO.FileAttributes.NotContentIndexed
End If
If m_CompressedBoolean = False Then
If (.Attributes And IO.FileAttributes.Compressed) = IO.FileAttributes.Compressed Then .Attributes = .Attributes Xor IO.FileAttributes.Compressed 'Turn off
Else
.Attributes = .Attributes Or System.IO.FileAttributes.Compressed
End If
End With
fi = Nothing
Catch : End Try
VB.NET:
Try
Dim fi As New FileInfo(element)
With fi
.Attributes = If(m_ArchiveBoolean = False AndAlso (.Attributes And IO.FileAttributes.Archive) = IO.FileAttributes.Archive, .Attributes Xor IO.FileAttributes.Archive, .Attributes Or IO.FileAttributes.Archive)
.Attributes = If(m_HiddenBoolean = False AndAlso (.Attributes And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden, .Attributes Xor IO.FileAttributes.Hidden, .Attributes Or IO.FileAttributes.Hidden)
.Attributes = If(m_ReadOnlyBoolean = False AndAlso (.Attributes And IO.FileAttributes.ReadOnly) = IO.FileAttributes.ReadOnly, .Attributes Xor IO.FileAttributes.ReadOnly, .Attributes Or IO.FileAttributes.ReadOnly)
.Attributes = If(m_IndexedBoolean = True AndAlso (.Attributes And IO.FileAttributes.NotContentIndexed) = IO.FileAttributes.NotContentIndexed, .Attributes Xor IO.FileAttributes.NotContentIndexed, .Attributes Or IO.FileAttributes.NotContentIndexed)
.Attributes = If(m_CompressedBoolean = False AndAlso (.Attributes And IO.FileAttributes.Compressed) = IO.FileAttributes.Compressed, .Attributes Xor IO.FileAttributes.Compressed, .Attributes Or IO.FileAttributes.Compressed)
End With
fi = Nothing
Catch : End Try
Edit: What the overall goal of this is that if the user has the attribute checkbox checked then the file needs to have it set, other wise the attribute (if it's present) needs to be unset.