Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...