无法删除以前由应用程序在 Win 7 上的 Qt 4.8 中创建的文件
Can't remove file previously created by the application in Qt 4.8 on Win 7
我已经从 Qt-ressources 复制了一个 .bat 文件到文件系统并执行了它。
之后我想删除该文件,但它在 Qt 中失败了。如果我重新启动应用程序时也失败。但是,可以在文件资源管理器中删除该文件。
我尝试了 QFile::remove
以及 QDir::remove
。静态和非静态版本 - 没有效果。
我尝试使用本机文件分隔符进行调用 - 也没有帮助。
这段代码有什么问题?
if ( QFileInfo( dataRootPath+"/backupdb.bat" ).exists() )
{
//debugger stepps in
QFile f( QFileInfo( dataRootPath+"/backupdb.bat" ).canonicalFilePath());
f.remove( );
}
我在将文件从资源复制到文件系统并在之后尝试将其删除时遇到了同样的问题。 QFile::errorString() returns "Access denied"。所以看起来资源文件有一些被QFile::copy复制的讨厌的权限。可能可以更改权限,但我使用了自己的 2 个函数来复制文件:
bool copyTextFile(QString srcPath, QString dstPath)
{
QFile file(srcPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
return writeTextFile(QString::fromUtf8(file.readAll()), dstPath);
}
bool writeTextFile(QString data, QString dstPath)
{
QFile file(dstPath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
QTextStream stream(&file);
stream << data;
return true;
}
我在删除它之前更改了它的权限。
QFile::copy(":/res/1.txt", "D:\1.txt");
QFile file("D:\1.txt");
file.setPermissions(file.permissions() |
QFileDevice::WriteOwner |
QFileDevice::WriteUser |
QFileDevice::WriteGroup |
QFileDevice::WriteOther);
file.remove();
我遇到了同样的错误,但就我而言,发布的解决方案不起作用。然而,事实证明我在我的代码中创建了一个未关闭的 std::ofstream
对象。因此,这使源文件保持打开状态,从而阻止了 Windows.
上的复制
我已经从 Qt-ressources 复制了一个 .bat 文件到文件系统并执行了它。 之后我想删除该文件,但它在 Qt 中失败了。如果我重新启动应用程序时也失败。但是,可以在文件资源管理器中删除该文件。
我尝试了 QFile::remove
以及 QDir::remove
。静态和非静态版本 - 没有效果。
我尝试使用本机文件分隔符进行调用 - 也没有帮助。
这段代码有什么问题?
if ( QFileInfo( dataRootPath+"/backupdb.bat" ).exists() )
{
//debugger stepps in
QFile f( QFileInfo( dataRootPath+"/backupdb.bat" ).canonicalFilePath());
f.remove( );
}
我在将文件从资源复制到文件系统并在之后尝试将其删除时遇到了同样的问题。 QFile::errorString() returns "Access denied"。所以看起来资源文件有一些被QFile::copy复制的讨厌的权限。可能可以更改权限,但我使用了自己的 2 个函数来复制文件:
bool copyTextFile(QString srcPath, QString dstPath)
{
QFile file(srcPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
return writeTextFile(QString::fromUtf8(file.readAll()), dstPath);
}
bool writeTextFile(QString data, QString dstPath)
{
QFile file(dstPath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
QTextStream stream(&file);
stream << data;
return true;
}
我在删除它之前更改了它的权限。
QFile::copy(":/res/1.txt", "D:\1.txt");
QFile file("D:\1.txt");
file.setPermissions(file.permissions() |
QFileDevice::WriteOwner |
QFileDevice::WriteUser |
QFileDevice::WriteGroup |
QFileDevice::WriteOther);
file.remove();
我遇到了同样的错误,但就我而言,发布的解决方案不起作用。然而,事实证明我在我的代码中创建了一个未关闭的 std::ofstream
对象。因此,这使源文件保持打开状态,从而阻止了 Windows.