tags:

views:

81

answers:

1

The task that I'm trying to handle is to create a set of wrappers around WINAPI to handle various file modifications for files with deep paths in Windows.

Currently, I copy files using WINAPI call prefixed with "\\?\" as suggested here. After copying the file, I'm using the file time metadata (created, accessed, modified) from the original file and setting the time on the destination file. I'm having a problem when I try to apply this logic to a readonly file.

This is what I've attempted so far:

  1. Copy the file.
  2. Remove the ReadOnly flag from the destination using SetFileAttributes after calling GetFileAttributes.
  3. GetFileTime(original)
  4. SetFileTime(destination) (with original file times)
  5. Apply the readonly flag again.

The problem with this is that the file modified time gets updated after reapplying the readonly flag. Is there a way to do this perserving all the metadata?

+5  A: 

I would try opening the file not for GENERIC_WRITE, but for FILE_WRITE_ATTRIBUTES, to set the file time, and expect that opening is granted even if the file has the read-only attribute set.

Martin v. Löwis
I've verified that Martin's suggestion does work.
Michael Burr
This sounds like just what I need. I won't be able to verify for couple of days (thanks Michael).
llamaoo7