cvz
cvz

Reputation: 139

QT Debugging QFile::remove() Windows 10 MSVS 2019

I am working on a QT application that has to communicate with a few Windows-utilities. The result of these utilities are a couple of “production-files” that should be listed in a “filenames-txt-file” for further use.

The production of such a “filenames-txt-file” with the list of “production-files” is done with QT using QFileInfo.
Sometimes an old “filenames-txt-file” already exists in the working-directory and should be removed before it can be created with the new results.

Here is the problem:
QFile::remove("somefile") does not work while debugging.
It works fine, when I run the Exe in the debug-folder outside MSVS,
and it works fine running the release version.

While debugging, I get this messages:

if (QFile::exists(filenameFull)) {
    QFile f (filenameFull);     
    qDebug() << f.remove(filenameFull);     // returns false
    qDebug() << f.errorString();            // returns “unknown error”
}

I elevated Microsoft Visual Studio 2019 to run as administrator.
I did set the UAC execution level to “highestAvailable”.
Is anything else needed to make this code working while debugging?

Upvotes: 0

Views: 128

Answers (3)

cvz
cvz

Reputation: 139

Solved thanks to @hyde. The issue was indeed that filenameFull was opened all the time.
Not in the code itself, but in the Configuration Properties -> Debugging -> Command Arguments
It was hanging there from an earlier stage in the coding process.
It explains as well why running the EXE outside the debugging-process was running fine.
Lesson: keep my coding steps clear at all times....
Thanks for getting me back on track.

Upvotes: 0

hyde
hyde

Reputation: 62888

You are calling static function, QFile::remove(QString filename), so f does not see the error. So try this:

if (QFile::exists(filenameFull)) {
    QFile f (filenameFull);
    qDebug() << f.exists();
    qDebug() << f.remove();      
    qDebug() << f.errorString();
}

That should solve the part about not getting error string.

Upvotes: 0

mugiseyebrows
mugiseyebrows

Reputation: 4743

It may be unrelated but instead f.remove(filenameFull) you can just use f.remove() or QFile::remove(filenameFull).

QFile::remove on windows uses winapi function DeleteFileW(("\\\\?\\" + QDir::toNativeSeparators(filenameFull)).utf16()) where "\\?\" is used to avoid MAX_PATH limitation. Try calling this function directly and analyze result.

Upvotes: 0

Related Questions