Qt C++ remove a read only file in windows using

后端 未结 6 925
挽巷
挽巷 2021-02-19 04:19

I have set a file to be read-only (right click and check readonly). Now when I try to remove the file using the function bool QDir::remove(const QString & fileName)

相关标签:
6条回答
  • 2021-02-19 04:48

    You can set file permissions with QFile

    Of course this only for files you have user permission to do. The error may also be because the file is open in another app

    0 讨论(0)
  • 2021-02-19 04:56
    file.setPermissions(QFile::ReadOther | QFile::WriteOther);
    file.remove();
    

    should work.

    0 讨论(0)
  • 2021-02-19 05:02

    First, have you checked QFile::error() to see why the file wasn't removed?

    Second, in the event that you're still not getting a useful error message back, you could check the source to find out if you can get more information. Checking the source reveals the following, for example:

    QFile::remove() uses the underlying file engine to do the removal. That file engine is platform specific and in qfsfileengine_win.cpp for windows. Line 830 shows that it's using DeleteFile to do the removal so you might be able to get more information by calling GetLastError, though I'd hope that Qt translates the error message appropriately.

    0 讨论(0)
  • 2021-02-19 05:07

    QDir::remove() function is not a static function. so you can create QDir with parent file path and call then remove it:

    QDir dir(parent's directory);

    and then

    dir.remove(fileName);

    0 讨论(0)
  • 2021-02-19 05:09

    I think you should use this:

    bool QFile::remove ( const QString & fileName ) [static]
    

    instead of this:

    QDir::remove ( const QString & fileName ) 
    
    0 讨论(0)
  • 2021-02-19 05:12

    Have you tried to use bool QFile::remove(const QString &fileName)?

    0 讨论(0)
提交回复
热议问题