12

In my MFC application I have set the read only attribute on a particular file. I have done this by using the SetFileAttributes() function. At some point I have to remove that attribute of that file again.

Can anyone explain how to do this?

Deanna
  • 23,876
  • 7
  • 71
  • 156
JijeshKV
  • 670
  • 2
  • 7
  • 26
  • 1
    Don't you just GetFileAttributes, mask off the flag (flags &=~READ_ONLY) and SetFileAttributes again? – Rup Oct 27 '11 at 09:26

1 Answers1

27

Use SetFileAttributes again to reset the flag:

SetFileAttributes( pszFilename,  
                   GetFileAttributes(pszFilename) & ~FILE_ATTRIBUTE_READONLY);

Might be worth adding that this method returns 0 if the function fails and you can use GetLastError().

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
  • 2
    GetFileAttributes() may returned many other flags that the SetFileAttributes() function doesn't understand, is that safe? Will those extra flag be ignored? – Alexis Wilke Dec 29 '12 at 04:58