Maxie
Maxie

Reputation: 98

rename(): Access Denied despite full control

The problem at first glance looks ridiculous. I am writing a simple application in C++. I compiled code. Everything worked perfectly, until I moved the exe to a different partition than the system. I have two partition (C: with system and E: with data, they both are NTFS), from C:, eg. from Desktop, from root of C:\ etc., program doesn't have any problems. From E:\, rename() function throws an error "Access Denied".

To clarify:
- I have full admin rights
- The program is run with administrative rights
- Transferred files aren't used by any process
- The system partition both as E: \ in security policy has granted full rights to the Administrators group and for me personally.

How to solve it?
For example: Games that can save files in the same place doesn't have a problem with that, even without administrator rights.

When I tried uses MoveFileEx I got error with code 5. Code 5 means "Access denied". Also because it's an other partition

Upvotes: 0

Views: 4526

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155634

Based on comments, your original and new names aren't on the same partition/drive/mount point. rename only changes names, it's not actually a "move" command (it just happens to be usable as such when the source and dest are on the same file system).

If you need to move across partitions, on Windows, you can use the MoveFile function which will either rename if possible, or "copy and delete original" when the destination is on a different partition.

If you need to stomp existing files (MoveFile refuses to overwrite an existing file), use MoveFileEx with the flags MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING (optionally or-ing in MOVEFILE_WRITE_THROUGH so the operation blocks until the file finishes being moved when an atomic rename isn't possible).

Upvotes: 3

Related Questions