SIMIN Posted June 28, 2008 Posted June 28, 2008 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 it's 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 won't work, it always returns that the file is locked, even if the file is archive only! What should I do? 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 Quote
MrPaul Posted June 29, 2008 Posted June 29, 2008 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#): 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: Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.