file attribute question

SIMIN

Well-known member
Joined
Mar 10, 2008
Messages
92
Hello all,
I want to save a file, so I show user save file dialog.
But when user selects an existing file and confirm its overwrite, I cannot simply set its attribute to normal and delete it, then overwrite it!
I have to check if its not either read only or hidden or system, then delete and overwrite it, am I right?
So I use this code to do the check, but it wont work, it always returns that the file is locked, even if the file is archive only!
What should I do?
Code:
If My.Computer.FileSystem.FileExists(SaveFileDialog.FileName) Then
    If System.IO.File.GetAttributes(SaveFileDialog.FileName) <> FileAttributes.Archive And System.IO.File.GetAttributes(SaveFileDialog.FileName) <> FileAttributes.Normal Then
        MessageBox.Show("The output file is locked." + vbNewLine + "Please use a different file name.", My.Application.Info.AssemblyName, MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Exit Sub
    End If
End If
 
Use bitwise comparison

FileAttributes is a flags-type enumeration, meaning variables of that type can contain by a bitwise combination of multiple values. You should therefore test for certain attributes using the bitwise And operator (& in C#):

Code:
Dim attrs As FileAttributes = File.GetAttributes(SaveFileDialog.FileName)
If ((attrs And FileAttributes.Hidden) = FileAttributes.Hidden) OrElse _
   ((attrs And FileAttributes.System) = FileAttributes.System) OrElse _
   ((attrs And FileAttributes.ReadOnly) = FileAttributes.ReadOnly)) Then
    File is hidden, system, or read only
End If

Good luck :cool:
 
Back
Top