PDA

View Full Version : QFile or QDir rename()



maxpower
23rd January 2009, 15:38
When I use on of the rename functions and a file is open by an outside program, a new file is created with the new name instead of the rename function returning false as i had hoped. Is there a way around this? A way to check if the file is open elsewhere? I tried using QFile:: open() but that still opened and then corrupted my file.

Qt 4.4.3 on Windows 2003.

mAx

seneca
23rd January 2009, 16:32
Qt tries to be smart and emulate unix-like rename across directories on windows, e.g. when operating system rename fails it tries a copy/delete, but does not check completely for success on deleting the old file.

As a workaround I suggest to make your own function. Something along this should work (did not test myself though):


#include "qt_windows.h"

bool winRenameFile(const QString& aOldName, const QString& aNewName)
{
return MoveFile((LPWSTR)aOldName.utf16(), (LPWSTR)aNewName.utf16());
} // winRenameFile

maxpower
23rd January 2009, 17:50
Thanks, that worked as I expected.

mAx